yinzhili 发表于 2009-12-23 09:52:14

修改User-Agent配置文件使User-Agent 类更加智能

本帖最后由 yinzhili 于 2009-12-23 09:56 编辑

有时候我们要用到User-Agent 类来收集一些访问者的信息,但是默认情况下User-Agent 类的配置文件并不是十分准确,比如说,默认情况下无法识别出IE的版本,所以我们可以通过修改配置文件来达到目的。

User-Agent 类的配置文件位于以下位置: application/config/user-agents.php ,打开它,找到$browsers数组
$browsers = array(
   'Opera'    => 'Opera',
   'MSIE'    => 'Internet Explorer',
   'Internet Explorer' => 'Internet Explorer',      
   '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'
    );

实际上程序是通过$_SERVER['HTTP_USER_AGENT']的值来获取访问者信息的。我们会发现默认情况下无论你使用IE7还是IE8,程序都会识别为Internet Explorer,此外Google Chrome被识别为Safari。因此为了使User-Agent类能识别出IE的常见版本以及Chrome,我们可以这样修改:
$browsers = array(
   'Opera'    => 'Opera',
   'MSIE 8.0' => 'Internet Explorer 8',
   'MSIE 7.0' => 'Internet Explorer 7',
   'MSIE 6.0' => 'Internet Explorer 6',
   'MSIE 5.5' => 'Internet Explorer 5.5',
   'MSIE'    => 'Internet Explorer',
   'Internet Explorer' => 'Internet Explorer',   
   'Chrome'                => 'Chrome',
   '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'
    );
这样,$this->agent->browser()方法就能正确地返回IE的版本,也能正常识别出Chrome。

还有一个问题,那就是User-Agent类还无法识别出Windows 7,这也难怪,CI的开发组编写代码的时候Windows 7还未发布。要解决也很简单,在$platforms数组中增加Windows 7的版本号就可以了,类似这样:

$platforms = array (
         'windows nt 6.1'      => 'Windows 7',
         'windows nt 6.0'      => 'Windows Vista',
         'windows nt 5.2'      => 'Windows 2003',
         'windows nt 5.0'      => 'Windows 2000',
         'windows nt 5.1'      => 'Windows XP',
         'windows nt 4.0'      => 'Windows NT 4.0',
         'winnt4.0'                        => 'Windows NT 4.0',
         'winnt 4.0'                        => 'Windows NT',
         'winnt'                              => 'Windows NT',
         'windows 98'                => 'Windows 98',
         'win98'                              => 'Windows 98',
         'windows 95'                => 'Windows 95',
         'win95'                              => 'Windows 95',
         'windows'                        => 'Unknown Windows OS',
         'os x'                              => 'Mac OS 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'
);


此外,我们还可以通过修改$robots数组来增加一些国内搜索引擎机器人的识别,例如:

$robots = array(
      'googlebot'                     => 'Googlebot',
      'msnbot'                        => 'Bing',
      'slurp'                            => 'Inktomi Slurp',
      'yahoo'                           => 'Yahoo',
      'askjeeves'                      => 'AskJeeves',
      'fastcrawler'                => 'FastCrawler',
      'infoseek'                  => 'InfoSeek Robot 1.0',
      'lycos'                           => 'Lycos',
      'baiduspider'                     =>'Baidu',
      'sosospider'                     =>'Soso',
      'youdaobot'                     =>'Youdao'
);

snllll 发表于 2010-8-13 00:58:46

我用CI做了一个WAP访问的页面,但是搜索引擎抓取的很好,很多用户来自PC,所以我正在想法识别pc用户,然后将他们转向正常浏览页面,楼主分享的东西对我非常有用,深表谢意。
页: [1]
查看完整版本: 修改User-Agent配置文件使User-Agent 类更加智能