Re: Mozilla user agent escapes detection
| From: | Shawn McKenzie | Date: | Tue, 02 Sep 2008 15:19:58 +0000 |
| Subject: | Re: Mozilla user agent escapes detection | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-279285@lists.php.net to get a copy of this message | ||
Jochem Maas wrote:
Dave M G schreef:Look at the code: strtolower('Mozilla') === 'mozilla' -ShawnPHP List, I have a script, part of which is taken from a script I found on the 'net, which tries to detect the user agent of the browser accessing the site. One of the reasons I'm doing this is to find out if the browser is a desktop or mobile. Part of the code looks like this: $ua = strtolower($_SERVER['HTTP_USER_AGENT']); $isDesktop = (strpos($ua, 'firefox') !== false) || (strpos($ua,'mosaic') !== false) || (strpos($ua,'msie') !== false) || (strpos($ua,'opera') !== false) || (strpos($ua,'mozilla') !== false) || (strpos($ua,'w3c_css_validator') !== false) || (strpos($ua,'w3c_validator') !== false); What's driving me crazy is that it successfully determines all the browsers listed there, *except* mozilla.Mozilla !== mozilla
stripos is the case-insenstive version of strposI have a log file which alerts me when an unknown browser comes through my site, and it's chock full of messages telling me that a Mozilla based browser has come by. There are a *lot* of Mozilla based browsers. Some examples from my logs of the browsers slipping by my test: HTTP_USER_AGENT: Mozilla/5.0 compatible; Yahoo! Slurp HTTP_USER_AGENT: Mozilla/5.0 compatible; Googlebot/2.1these 2 are bots btw.HTTP_USER_AGENT: Mozilla/5.0 X11; U; Linux i686 In each case, the strpos() should have seen the mozilla agent and not bothered logging it. It's not complicated code, especially considering it's successfully finding msie and firefox. I can't imagine why Mozilla would be different. Any suggestions? Is there something about my code right in front of my face that I'm not seeing? Any suggestions would be greatly appreciated.STW? plenty of people offering code that attempts to detect mobile devices, e.g.: http://www.andymoore.info/php-to-detect-mobile-phones/ also I'd offer a link in the top of your site pointing to the mobile site (and vice-versa) in order to: 1. allow bots to visit both. 2. in case your code guessed incorrectly for some user(s) I'd also check the UA string specifically for bots first and just let them through ... they are neither desktop nor mobile browsers. additionally you might consider how you want to accomodate text browsers (which is kind of what a bot is)Thanks.