php检测useragent版本示例
<?php
define('ie', 1);
define('firefox', 2);
define('chrome', 3);
define('opera', 4);
class useragentdetect
{
static $version = array();
static $useragent = '';
static function getuseragent()
{
$header = getallheaders();
self :: $useragent = $header['user-agent'];
}
function finduseragentbykey($ua, $key)
{
$len = strlen($key);
$start = strpos($ua, $key);
$pos = strpos($ua, ' ', $start + $len + 1);
return substr($ua, $start + $len, $pos - $len - $start);
}
static function detectuseragent()
{
self :: getuseragent();
if (strpos(self :: $useragent, 'msie') != null)
{
self :: $version[] = ie;
self :: $version[] =self:: finduseragentbykey(self :: $useragent, 'msie');
}
else if (strpos(self :: $useragent, 'gecko/') != null)
{
self :: $version[] = firefox;
self :: $version[] =self:: finduseragentbykey(self :: $useragent, 'firefox/');
}
else if (strpos(self :: $useragent, 'applewebkit/') != null)
{
self :: $version[] = chrome;
self :: $version[] = self:: finduseragentbykey(self :: $useragent, 'chrome/');
}
else if (strpos(self :: $useragent, 'presto/') != null)
{
self :: $version[] = opera;
self :: $version[] = self:: finduseragentbykey(self :: $useragent, 'opera/');
}
else
{
}
return self :: $version;
}
function matchuseragent($ua, $key)
{
preg_match("/$key.([^\s]*?)\s/", $ua, $out);
return $out[1];
}
}
$ua=useragentdetect::detectuseragent();
print_r($ua);
上一篇: Python模拟登录12306的方法