Shawn McKenzie schreef:
Jochem Maas wrote:
Dave M G schreef:
PHP 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
Look at the code: strtolower('Mozilla') === 'mozilla'
oh crap :-)
-Shawn
stripos is the case-insenstive version of strpos
I 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.1
these 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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php