Forum Moderators: coopster
A few searches revealed a couple of basic browser user-agent detection functions, although nothing under 20 lines of code or so.
All I want to do is:-
If Not(FireFox) display GetFirefox adsense button
Is there a gloriously simple way of doing that, or do I need to use one of these GPL script functions out there?
Thanks,
TJ
if (!stripos [php.net]($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
// display GetFirefox adsense button
}
assigning a variable using getenv()
versus
assigning a variable using the $_SERVER superglobal
and
discovering the user agent with each of the following:
eregi
stripos
strpos(strtolower())
preg_match
Conclusions on my PHP5 box at the end of the code (note: the snippet won't work unless you are on PHP5 because of the microtime function, you'll have to modify it to work in PHP4) ...
<?php
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
$UA=getenv("HTTP_USER_AGENT");
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("getenv Time: %0.15f\n", $time);
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
$UA=$_SERVER['HTTP_USER_AGENT'];
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("SERVER Time: %0.15f\n", $time);
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
if(!eregi("Firefox",$_SERVER['HTTP_USER_AGENT'])){
// display GetFirefox adsense button
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf("eregi Time: %0.15f\n", $time);
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
if (!stripos($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
// display GetFirefox adsense button
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf(">PHP5 Time: %0.15f\n", $time);
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
if (!strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'firefox')) {
// display GetFirefox adsense button
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf(">PHP4 Time: %0.15f\n", $time);
$time_start = microtime(true);
for ($i=1; $i<=1000; $i++) {
if(!preg_match("/Firefox/i",$_SERVER['HTTP_USER_AGENT'])){
// display GetFirefox adsense button
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
printf(">pcre Time: %0.15f\n", $time);
exit;
getenv Time: 0.013556957244873
SERVER Time: 0.000906944274902
eregi Time: 0.015411138534546
>PHP5 Time: 0.009479045867920
>PHP4 Time: 0.008875131607056
>pcre Time: 0.003269910812378
//
// After applying a static var:
//
eregi Time: 0.017683029174805
>PHP5 Time: 0.008775949478149
>PHP4 Time: 0.009084939956665
>pcre Time: 0.003593921661377
I had never tested getenv() vs $_SERVER before so I thought I would see what showed up ... using the $_SERVER superglobal was much faster than using getenv(). Every time.
But this surprised me ...
preg_match() came in much faster than the other comparisons. I wondered if it had something to do with the "lookup" of the $_SERVER superglobal in each of the for loops so I assigned the $_SERVER['HTTP_USER_AGENT'] to a static variable in another test and it made no difference. What was also odd was that the "PHP5 stripos()" versus "PHP4 strpos(strtolower())" comparison bounced back and forth. They were so close in comparison that it made little or no difference. I haven't looked into the source code for the new PHP5 function stripos() but I am guessing that it is doing much the same behind the scenes as what we are doing here with the existing functions available in PHP4.
Too much time on my hands tonight I guess ;-)
*Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
Isn't that just going to lowercase the result of strpos?
Hehe - good spot Whoisgregg - thanks. That's one of those bugs that could have been hard to spot and work out what was going wrong ;-)
Coop thanks, this is fascinating. Based on this do I take it that pregmatch under php4 is probably going to serve me just as well as anything else?
TJ
I posted all the details here only because other PHP'ers may want to have a look and do some more conclusive studies themselves. We often discuss how certain functions out-perform others, particularly when the recommendations come from the PHP developers and the accompanying documentation. In this particular case I am noticing a turn in the trend so I thought I would post my findings.