codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: Why not check the browser name? #7418

Closed warmbook closed 1 year ago

warmbook commented 1 year ago

PHP Version

8.1

CodeIgniter4 Version

4.3.1

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

No response

What happened?

I add one row to the property 'browsers' of 'Config\UserAgents' class, the key is 'MicroMessenger' and the value is 'WeChat'. Then I want to know whether a HTTP request is from WeChat browser, so I called the 'isBrowser' method in my service code and the only argument is 'WeChat'. But it always returns false. I read the first floor code and find that the 'isBrowser' method only check if the key is exist.

Steps to Reproduce

Add one row to the property 'browsers' of 'Config\UserAgents' class, the key is 'MicroMessenger' and the value is 'WeChat'. Then call the 'isBrowser' method with 'WeChat'.

Expected Output

Return true when the header 'user-agent' contains 'MicroMessenger'.

Anything else?

Sometimes, we only want to know whether the HTTP request is from some browser, and don't care which version it is. So why not compare the argument '$key' with the property 'browser'? Just like this:

if ($key === null || $this->browser === $key) {
    return true;
}
kenjis commented 1 year ago

I don't know why the current behavior is like this. But it comes from CI3 and the parameter is $key that specifies the Config array key.

Therefore, the usage is:

$agent->isBrowser('MicroMessenger')
// or
$agent->getBrowser() === 'WeChat`
kenjis commented 1 year ago

We use GitHub issues to track BUGS and to track approved DEVELOPMENT work packages. We use our forum to provide SUPPORT and to discuss FEATURE REQUESTS.