-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: How can I know which browser name is the request from? #8930
Comments
Call CodeIgniter4/app/Config/UserAgents.php Line 81 in 482b8bb
|
In a controller: $agent = $this->request->getUserAgent();
d($agent->getBrowser()); |
Ah, the following $agent = $this->request->getUserAgent();
d($agent->getBrowser());
echo($agent->getAgentString());
dd($agent->isBrowser('Edge'));
|
@codeigniter4/core-team But it is actually the key of CodeIgniter4/system/HTTP/UserAgent.php Lines 310 to 323 in 482b8bb
After all, the current implementation (line 133) seems to be incorrect. CodeIgniter4/system/HTTP/UserAgent.php Lines 121 to 134 in 482b8bb
|
The user guide states:
So, I would say we're good. |
Yes, the Note says it is an array key. |
Yes, It's indeed difficult to use/understand. Especially when some browsers can't be specifyed by pure letter string ,such as QQ, we must write code |
Perhaps, but the behavior is the same since probably v2. There is no bug, the method works correctly. Feel free to suggest a better option (preferably with PR), but please make it an alternative. The current behavior should remain unchanged - too many apps may rely on it. |
In my humble opinion, change the statement "if($key === null)" to "if ($key === null||$this->browser === $key)" may not affect apps rely on it? |
I kindly disagree. If we start matching the result based on both: the array key and the value, there is no chance to determine which value was matched. "Edge" is a great example of this: 'Edge' => 'Spartan',
'Edg' => 'Edge', If we change the code according to your suggestion, it will give unexpected results. $agent->isBrowser('Edge'); If the above code returns To check a browser by its name, I suggest you simply use: $agent->getBrowser() === 'Edge' |
|
@michalsn Indeed, if we think the parameter for But if we want to check if it is "Spartan", we must write: $agent->isBrowser('Edge') and check if it is "Edge", we must write: $agent->isBrowser('Edg') This seems too tricky. It seems the following is better and simpler: --- a/system/HTTP/UserAgent.php
+++ b/system/HTTP/UserAgent.php
@@ -130,7 +130,7 @@ class UserAgent implements Stringable
}
// Check for a specific browser
- return isset($this->config->browsers[$key]) && $this->browser === $this->config->browsers[$key];
+ return $this->browser === $key;
}
/** |
@kenjis It might be less intuitive, but this is how it was designed to work and it's well-documented.
This change would completely break the code logic for some apps. Checking |
@michalsn Thanks! I have got it. |
PHP Version
8.1
CodeIgniter4 Version
4.5.0
CodeIgniter4 Installation Method
Manual (zip or tar.gz)
Which operating systems have you tested for this bug?
Linux
Which server did you use?
apache
Database
No response
What happened?
When I call the method CodeIgniter\HTTP\UserAgent->isBrowser with 'Spartan', if the client browser agent string matched the keyword 'Edge', the method will return false.
Steps to Reproduce
Call CodeIgniter\HTTP\UserAgent->isBrowser with 'Spartan', and request the server by a browser which's agent string includes 'Edge'.
Expected Output
method should return true
Anything else?
By viewing the source code,I found this method only check whether the param $key is existed in user agent string, but not compare it with the property 'browser'. It's not compliant with description in the manual, which say the param $key is 'Optional browser name'. Maybe you should change the statement "
if($key === null)
" to "if ($key === null||$this->browser === $key)
"?The other two method 'isRobot' and 'isMobile' also have the same problem.
The text was updated successfully, but these errors were encountered: