php 分解 user Agent 参数

class User_agent {

var $platforms = array (
‘windows nt 6.2’ => ‘Win8’,
‘windows nt 6.1’ => ‘Win7’,
‘windows nt 6.0’ => ‘Win Longhorn’,
‘windows nt 5.2’ => ‘Win2003’,
‘windows nt 5.0’ => ‘Win2000’,
‘windows nt 5.1’ => ‘WinXP’,
‘windows nt 4.0’ => ‘Windows NT 4.0’,
‘winnt4.0’ => ‘Windows NT 4.0’,
‘winnt 4.0’ => ‘Windows NT’,
‘winnt’ => ‘Windows NT’,
‘windows 98’ => ‘Win98’,
‘win98’ => ‘Win98’,
‘windows 95’ => ‘Win95’,
‘win95’ => ‘Win95’,
‘windows’ => ‘Unknown Windows OS’,
‘os x’ => ‘MacOS X’,
‘ppc mac’ => ‘Power PC Mac’,
‘freebsd’ => ‘FreeBSD’,
‘ppc’ => ‘Macintosh’,
‘linux’ => ‘Linux’,
‘debian’ => ‘Debian’,
‘sunos’ => ‘Sun Solaris’,
‘beos’ => ‘BeOS’,
‘apachebench’ => ‘ApacheBench’,
‘aix’ => ‘AIX’,
‘irix’ => ‘Irix’,
‘osf’ => ‘DEC OSF’,
‘hp-ux’ => ‘HP-UX’,
‘netbsd’ => ‘NetBSD’,
‘bsdi’ => ‘BSDi’,
‘openbsd’ => ‘OpenBSD’,
‘gnu’ => ‘GNU/Linux’,
‘unix’ => ‘Unknown Unix OS’
);
// The order of this array should NOT be changed. Many browsers return
// multiple browser types so we want to identify the sub-type first.
var $browsers = array(
‘Flock’ => ‘Flock’,
‘Chrome’ => ‘Chrome’,
‘Opera’ => ‘Opera’,
‘MSIE’ => ‘IE’,
‘Internet Explorer’ => ‘IE’,
‘Shiira’ => ‘Shiira’,
‘Firefox’ => ‘Firefox’,
‘Chimera’ => ‘Chimera’,
‘Phoenix’ => ‘Phoenix’,
‘Firebird’ => ‘Firebird’,
‘Camino’ => ‘Camino’,
‘Netscape’ => ‘Netscape’,
‘OmniWeb’ => ‘OmniWeb’,
‘Safari’ => ‘Safari’,
‘Mozilla’ => ‘Mozilla’,
‘Konqueror’ => ‘Konqueror’,
‘icab’ => ‘iCab’,
‘Lynx’ => ‘Lynx’,
‘Links’ => ‘Links’,
‘hotjava’ => ‘HotJava’,
‘amaya’ => ‘Amaya’,
‘IBrowse’ => ‘IBrowse’
);

var $mobiles = array(
// legacy array, old values commented out
‘mobileexplorer’ => ‘Mobile Explorer’,
// ‘openwave’ => ‘Open Wave’,
// ‘opera mini’ => ‘Opera Mini’,
// ‘operamini’ => ‘Opera Mini’,
// ‘elaine’ => ‘Palm’,
‘palmsource’ => ‘Palm’,
// ‘digital paths’ => ‘Palm’,
// ‘avantgo’ => ‘Avantgo’,
// ‘xiino’ => ‘Xiino’,
‘palmscape’ => ‘Palmscape’,
// ‘nokia’ => ‘Nokia’,
// ‘ericsson’ => ‘Ericsson’,
// ‘blackberry’ => ‘BlackBerry’,
// ‘motorola’ => ‘Motorola’

// Phones and Manufacturers
‘motorola’ => “Motorola”,
‘nokia’ => “Nokia”,
‘palm’ => “Palm”,
‘iphone’ => “Apple iPhone”,
‘ipad’ => “iPad”,
‘ipod’ => “Apple iPod Touch”,
‘sony’ => “Sony Ericsson”,
‘ericsson’ => “Sony Ericsson”,
‘blackberry’ => “BlackBerry”,
‘cocoon’ => “O2 Cocoon”,
‘blazer’ => “Treo”,
‘lg’ => “LG”,
‘amoi’ => “Amoi”,
‘xda’ => “XDA”,
‘mda’ => “MDA”,
‘vario’ => “Vario”,
‘htc’ => “HTC”,
‘samsung’ => “Samsung”,
‘sharp’ => “Sharp”,
‘sie-‘ => “Siemens”,
‘alcatel’ => “Alcatel”,
‘benq’ => “BenQ”,
‘ipaq’ => “HP iPaq”,
‘mot-‘ => “Motorola”,
‘playstation portable’ => “PlayStation Portable”,
‘hiptop’ => “Danger Hiptop”,
‘nec-‘ => “NEC”,
‘panasonic’ => “Panasonic”,
‘philips’ => “Philips”,
‘sagem’ => “Sagem”,
‘sanyo’ => “Sanyo”,
‘spv’ => “SPV”,
‘zte’ => “ZTE”,
‘sendo’ => “Sendo”,

// Operating Systems
‘symbian’ => “Symbian”,
‘SymbianOS’ => “SymbianOS”,
‘elaine’ => “Palm”,
‘palm’ => “Palm”,
‘series60’ => “Symbian S60”,
‘windows ce’ => “Windows CE”,

// Browsers
‘obigo’ => “Obigo”,
‘netfront’ => “Netfront Browser”,
‘openwave’ => “Openwave Browser”,
‘mobilexplorer’ => “Mobile Explorer”,
‘operamini’ => “Opera Mini”,
‘opera mini’ => “Opera Mini”,

// Other
‘digital paths’ => “Digital Paths”,
‘avantgo’ => “AvantGo”,
‘xiino’ => “Xiino”,
‘novarra’ => “Novarra Transcoder”,
‘vodafone’ => “Vodafone”,
‘docomo’ => “NTT DoCoMo”,
‘o2’ => “O2”,

// Fallback
‘mobile’ => “Generic Mobile”,
‘wireless’ => “Generic Mobile”,
‘j2me’ => “Generic Mobile”,
‘midp’ => “Generic Mobile”,
‘cldc’ => “Generic Mobile”,
‘up.link’ => “Generic Mobile”,
‘up.browser’ => “Generic Mobile”,
‘smartphone’ => “Generic Mobile”,
‘cellphone’ => “Generic Mobile”
);

// There are hundreds of bots but these are the most common.
var $robots = array(
‘googlebot’ => ‘Googlebot’,
‘msnbot’ => ‘MSNBot’,
‘slurp’ => ‘Inktomi Slurp’,
‘yahoo’ => ‘Yahoo’,
‘askjeeves’ => ‘AskJeeves’,
‘fastcrawler’ => ‘FastCrawler’,
‘infoseek’ => ‘InfoSeek Robot 1.0’,
‘lycos’ => ‘Lycos’
);
var $agent = NULL;

var $is_browser = FALSE;
var $is_robot = FALSE;
var $is_mobile = FALSE;

var $languages = array();
var $charsets = array();

var $platform = ”;
var $browser = ”;
var $version = ”;
var $mobile = ”;
var $robot = ”;

/**
* Constructor
*
* Sets the User Agent and runs the compilation routine
*
* @access public
* @return void
*/
public function __construct($agent = ”)
{
if ($agent != ”)
{
$this->agent = $agent;
}
else if (isset($_SERVER[‘HTTP_USER_AGENT’]))
{
$this->agent = trim($_SERVER[‘HTTP_USER_AGENT’]);
}

if ( ! is_null($this->agent))
{
$this->_compile_data();
}

//log_message(‘debug’, “User Agent Class Initialized”);
}

// ——————————————————————–

/**
* Compile the User Agent Data
*
* @access private
* @return bool
*/
private function _compile_data()
{
$this->_set_platform();

foreach (array(‘_set_robot’, ‘_set_browser’, ‘_set_mobile’) as $function)
{
if ($this->$function() === TRUE)
{
break;
}
}
}

// ——————————————————————–

/**
* Set the Platform
*
* @access private
* @return mixed
*/
private function _set_platform()
{
if (is_array($this->platforms) AND count($this->platforms) > 0)
{
foreach ($this->platforms as $key => $val)
{
if (preg_match(“|”.preg_quote($key).”|i”, $this->agent))
{
$this->platform = $val;
return TRUE;
}
}
}
$this->platform = ‘Unknown Platform’;
}

// ——————————————————————–

/**
* Set the Browser
*
* @access private
* @return bool
*/
private function _set_browser()
{
if (is_array($this->browsers) AND count($this->browsers) > 0)
{
foreach ($this->browsers as $key => $val)
{
if (preg_match(“|”.preg_quote($key).”.*?([0-9\.]+)|i”, $this->agent, $match))
{
$this->is_browser = TRUE;
$this->version = $match[1];
$this->browser = $val;
$this->_set_mobile();
return TRUE;
}
}
}
return FALSE;
}

// ——————————————————————–

/**
* Set the Robot
*
* @access private
* @return bool
*/
private function _set_robot()
{
if (is_array($this->robots) AND count($this->robots) > 0)
{
foreach ($this->robots as $key => $val)
{
if (preg_match(“|”.preg_quote($key).”|i”, $this->agent))
{
$this->is_robot = TRUE;
$this->robot = $val;
return TRUE;
}
}
}
return FALSE;
}

// ——————————————————————–

/**
* Set the Mobile Device
*
* @access private
* @return bool
*/
private function _set_mobile()
{
if (is_array($this->mobiles) AND count($this->mobiles) > 0)
{
foreach ($this->mobiles as $key => $val)
{
if (FALSE !== (strpos(strtolower($this->agent), $key)))
{
$this->is_mobile = TRUE;
$this->mobile = $val;
return TRUE;
}
}
}
return FALSE;
}

// ——————————————————————–

/**
* Set the accepted languages
*
* @access private
* @return void
*/
private function _set_languages()
{
if ((count($this->languages) == 0) AND isset($_SERVER[‘HTTP_ACCEPT_LANGUAGE’]) AND $_SERVER[‘HTTP_ACCEPT_LANGUAGE’] != ”)
{
$languages = preg_replace(‘/(;q=[0-9\.]+)/i’, ”, strtolower(trim($_SERVER[‘HTTP_ACCEPT_LANGUAGE’])));

$this->languages = explode(‘,’, $languages);
}

if (count($this->languages) == 0)
{
$this->languages = array(‘Undefined’);
}
}

// ——————————————————————–

/**
* Set the accepted character sets
*
* @access private
* @return void
*/
private function _set_charsets()
{
if ((count($this->charsets) == 0) AND isset($_SERVER[‘HTTP_ACCEPT_CHARSET’]) AND $_SERVER[‘HTTP_ACCEPT_CHARSET’] != ”)
{
$charsets = preg_replace(‘/(;q=.+)/i’, ”, strtolower(trim($_SERVER[‘HTTP_ACCEPT_CHARSET’])));

$this->charsets = explode(‘,’, $charsets);
}

if (count($this->charsets) == 0)
{
$this->charsets = array(‘Undefined’);
}
}

// ——————————————————————–

/**
* Is Browser
*
* @access public
* @return bool
*/
public function is_browser($key = NULL)
{
if ( ! $this->is_browser)
{
return FALSE;
}

// No need to be specific, it’s a browser
if ($key === NULL)
{
return TRUE;
}

// Check for a specific browser
return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
}

// ——————————————————————–

/**
* Is Robot
*
* @access public
* @return bool
*/
public function is_robot($key = NULL)
{
if ( ! $this->is_robot)
{
return FALSE;
}

// No need to be specific, it’s a robot
if ($key === NULL)
{
return TRUE;
}

// Check for a specific robot
return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
}

// ——————————————————————–

/**
* Is Mobile
*
* @access public
* @return bool
*/
public function is_mobile($key = NULL)
{
if ( ! $this->is_mobile)
{
return FALSE;
}

// No need to be specific, it’s a mobile
if ($key === NULL)
{
return TRUE;
}

// Check for a specific robot
return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
}

// ——————————————————————–

/**
* Is this a referral from another site?
*
* @access public
* @return bool
*/
public function is_referral()
{
if ( ! isset($_SERVER[‘HTTP_REFERER’]) OR $_SERVER[‘HTTP_REFERER’] == ”)
{
return FALSE;
}
return TRUE;
}

// ——————————————————————–

/**
* Agent String
*
* @access public
* @return string
*/
public function agent_string()
{
return $this->agent;
}

// ——————————————————————–

/**
* Get Platform
*
* @access public
* @return string
*/
public function platform()
{
return $this->platform;
}

// ——————————————————————–

/**
* Get Browser Name
*
* @access public
* @return string
*/
public function browser()
{
return $this->browser;
}

// ——————————————————————–

/**
* Get the Browser Version
*
* @access public
* @return string
*/
public function version()
{
return $this->version;
}

// ——————————————————————–

/**
* Get The Robot Name
*
* @access public
* @return string
*/
public function robot()
{
return $this->robot;
}
// ——————————————————————–

/**
* Get the Mobile Device
*
* @access public
* @return string
*/
public function mobile()
{
return $this->mobile;
}

// ——————————————————————–

/**
* Get the referrer
*
* @access public
* @return bool
*/
public function referrer()
{
return ( ! isset($_SERVER[‘HTTP_REFERER’]) OR $_SERVER[‘HTTP_REFERER’] == ”) ? ” : trim($_SERVER[‘HTTP_REFERER’]);
}

// ——————————————————————–

/**
* Get the accepted languages
*
* @access public
* @return array
*/
public function languages()
{
if (count($this->languages) == 0)
{
$this->_set_languages();
}

return $this->languages;
}

// ——————————————————————–

/**
* Get the accepted Character Sets
*
* @access public
* @return array
*/
public function charsets()
{
if (count($this->charsets) == 0)
{
$this->_set_charsets();
}

return $this->charsets;
}

// ——————————————————————–

/**
* Test for a particular language
*
* @access public
* @return bool
*/
public function accept_lang($lang = ‘en’)
{
return (in_array(strtolower($lang), $this->languages(), TRUE));
}

// ——————————————————————–

/**
* Test for a particular character set
*
* @access public
* @return bool
*/
public function accept_charset($charset = ‘utf-8’)
{
return (in_array(strtolower($charset), $this->charsets(), TRUE));
}

}

 

#简单正则分离

$str = ‘Mozilla/5.0 (Linux; Android 6.0; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36 TTWebView/0621111013039 JsSdk/2 NewsArticle/7.3.7.14 NetType/wifi (NewsLite 7.3.9)’;

$str2 = ‘Mozilla/5.0 (iPhone; CPU iPhone OS 13.3.1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 NewsArticle/7.6.5.11 JsSdk/2.0 NetType/4G (News 7.6.5 13.300000)’;

$str3 = ‘Mozilla/5.0 (Linux; Android 5.0.2; vivo Y33 Build/LRX21M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36 JsSdk/2 NewsArticle/6.7.5 NetType/wifi (NewsLite 6.7.5)’;
$str4 =’Mozilla/5.0 (iPhone; CPU iPhone OS 13.3.5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 NewsArticle/7.6.5.11 ‘;
$str = strtolower($str);
$reg = ‘/^([a-zA-Z]+)\/([\d\.]+)\s+\(([a-zA-Z]+);\s+([a-zA-Z ]+)\s+([\d\.]+).*?\s+([a-zA-Z0-9-_\s]+)\s+.*[newsarticle|newslite]\/([\d\.]+)\s+/’;
preg_match($reg,$str,$result);
preg_match($reg,$str2,$result2);
preg_match($reg,$str3,$result3);
preg_match($reg,$str4,$result4);
var_dump($result,$result2,$result3,$result4);

发表评论