获取浏览器版本在php中直接使用HTTP_USER_AGENT就可以了,但是要具体判断是那个浏览器,什么版本我们需要一个个去判断了,实例代码如下:
- function get_user_browser() {
- if (emptyempty($_SERVER['HTTP_USER_AGENT'])) {
- return '';
- }
- $agent = $_SERVER['HTTP_USER_AGENT'];
- $browser = '';
- $browser_ver = '';
- if (preg_match('/MSIEs([^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('/NetCaptors([^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 (!emptyempty($browser)) {
- return addslashes($browser . ' ' . $browser_ver);
- } else {
- return 'Unknow browser';
- }
- }
实例二代码如下:
- function get_user_browser()
- {
- if (emptyempty($_SERVER['HTTP_USER_AGENT']))
- {
- return '';
- }
- $agent = $_SERVER['HTTP_USER_AGENT'];
- $browser = '';
- $browser_ver = '';
- if (preg_match('/MSIEs([^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('/NetCaptors([^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 (!emptyempty($browser))
- {
- return addslashes($browser . ' ' . $browser_ver);
- }
- else
- {
- return 'Unknow browser';
- }
- }
判断是否为蜘蛛:
- function is_spider($record = true)
- {
- static $spider = NULL;
- if ($spider !== NULL)
- {
- return $spider;
- }
- if (emptyempty($_SERVER['HTTP_USER_AGENT']))
- {
- $spider = '';
- return '';
- }
- $searchengine_bot = array(
- 'googlebot',
- 'mediapartners-google',
- 'baiduspider+',
- 'msnbot',
- 'yodaobot',
- 'yahoo! slurp;',
- 'yahoo! slurp china;',
- 'iaskspider',
- 'sogou web spider',
- 'sogou push spider'
- );
- $searchengine_name = array(
- 'GOOGLE',
- 'GOOGLE ADSENSE',
- 'BAIDU',
- 'MSN',
- 'YODAO',
- 'YAHOO',
- 'Yahoo China',
- 'IASK',
- 'SOGOU',
- 'SOGOU'
- );
- $spider = strtolower($_SERVER['HTTP_USER_AGENT']);
- foreach ($searchengine_bot AS $key => $value)
- {
- if (strpos($spider, $value) !== false)
- {
- $spider = $searchengine_name[$key];
- return $spider;
- }
- }
- $spider = '';
- return '';
- }
|