This example describes how PHP obtains the client browser name and version. Share to everyone for your reference, specific as follows:
See a function in Ecshop get_user_browser () to get the name and version of the browser. Although the information obtained is simply some information, it is still very useful. The principle is mainly through the $_server[' http_user_agent '] to obtain browser information, and then use the comparison to get the browser information.
The following are the effects of each browser run:
The source code is as follows:
<?php function Get_user_browser () {if (Empty ($_server[' http_user_agent '))) {return ';
} $agent = $_server[' http_user_agent '];
$browser = ';
$browser _ver = '; if (preg_match) ('/msie\s ([^\s|;]
+/I ', $agent, $regs)) {$browser = ' Internet Explorer ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/firefox\/([^\s]+)/I ', $agent, $regs)) {$browser = ' FireFox ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/maxthon/i ', $agent, $regs)) {$browser = ' (Internet Explorer '. $browser _ver. ') Maxthon ';
$browser _ver = ';
} elseif (Preg_match ('/opera[\s|\/] ([^\s]+)/I ', $agent, $regs)) {$browser = ' Opera ';
$browser _ver = $regs [1]; } elseif (Preg_match ('/omniweb\/v*) ([^\s|;]
+/I ', $agent, $regs)) {$browser = ' omniweb ';
$browser _ver = $regs [2];
} elseif (Preg_match ('/netscape ([\d]*) \/([^\s]+)/I ', $agent, $regs)) {$browser = ' Netscape ';
$browser _ver = $regs [2]; } ElseIf (Preg_match ('/safari\/([^\s]+)/I ', $agent, $regs)) {$browser = ' safari ';
$browser _ver = $regs [1]; } elseif (Preg_match ('/netcaptor\s) ([^\s|;]
+/I ', $agent, $regs)) {$browser = ' (Internet Explorer '. $browser _ver. ') Netcaptor ';
$browser _ver = $regs [1];
} elseif (Preg_match ('/lynx\/([^\s]+)/I ', $agent, $regs)) {$browser = ' Lynx ';
$browser _ver = $regs [1]; } if (!empty ($browser)) {return addslashes ($browser. ' ' .
$browser _ver);
else {return ' Unknow browser ';
} echo Get_user_browser ();?>
I hope this article will help you with your PHP programming.