This example describes how PHP gets the user browser version. Share to everyone for your reference. The specific analysis is as follows:
In PHP we have a global variable $_server[' http_user_agent '], we can get all the user information, we have to be processed to determine what type of user browser, the following function can be accurate user browser version number code.
function Getbrowse ()
{
$agent = $_server[' http_user_agent '];
$browser = ';
$browserver = ';
$browser = Array (' Lynx ', ' mosaic ', ' AOL ', ' opera ', ' Java ', ' Macweb ', ' webexplorer ', ' omniweb ');
for ($i = 0; $i <= 7; $i + +) {
if (Strpos ($agent, $browsers [$i])) {
$browser = $browsers [$i];
$browserver = ';
}
}
if (Ereg (' Mozilla ', $agent) &&!ereg (' Msie ', $agent)) {
$temp = Explode (' (', $agent);
$part = $temp [0];
$temp = explode ('/', $part);
$browserver = $temp [1];
$temp = Explode (", $browserver);
$browserver = $temp [0];
$browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
$browserver = $browserver;
$browser = ' Netscape Navigator ';
}
if (Ereg (' Mozilla ', $agent) && ereg (' opera ', $agent)) {
$temp = Explode (' (', $agent);
$part = $temp [1];
$temp = Explode (') ', $part);
$browserver = $temp [1];
$temp = Explode (", $browserver);
$browserver = $temp [2];
$browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
$browserver = $browserver;
$browser = ' opera ';
}
if (Ereg (' Mozilla ', $agent) && ereg (' Msie ', $agent)) {
$temp = Explode (' (', $agent);
$part = $temp [1];
$temp = explode ('; ', $part);
$part = $temp [1];
$temp = Explode (", $part);
$browserver = $temp [2];
$browserver = Preg_replace ('/([D.] +)/', ' \1 ', $browserver);
$browserver = $browserver;
$browser = ' Internet Explorer ';
}
if ($browser!= ') {
$browseinfo = $browser. ' '. $browserver;
} else {
$browseinfo = false;
}
return $browseinfo;
}
Application method
In IE
Echo Getbrowse (); Internet Explorer 6.0
In Firefox
Echo Getbrowse ();//netscape Navigator 5.0
I hope this article will help you with your PHP program design.