网络实验室

 找回密码
 注册账户
查看: 1163|回复: 1

PHP Functions for detecting Small Screen User Agents & Spiders

[复制链接]
无心的棋子 发表于 2008-9-10 01:40:01 | 显示全部楼层 |阅读模式
  1. <?php
  2. /*


  3. is_spider()
  4.         - Function for testing if a useragent matches that of a spider
  5.         - Returns useragent string if true

  6. is_smallscreen()
  7.         - Function for testing if a useragent matches that of a small screen
  8.           device, ie PDA or cellphone
  9.         - Returns useragent string if true
  10.         - Not really dependable, I couldn't find a comprehensive list of
  11.           user agents or anything in common, but good enough for me.





  12. Copyright (c) 2006, Takeshi Media

  13. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  14. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  16. */





  17. function is_spider()
  18. {
  19.         global $_SERVER;
  20.         static $isspider = 'init';

  21.         if( $isspider != 'init' )
  22.         {
  23. // its set so don't do anything
  24.         } else {
  25. // don't know if this is a spider or not yet
  26.                 $spiders = array(
  27.                 "almaden.ibm.com" ,
  28.                 "gigabot" ,
  29.                 "appie 1.1" ,
  30.                 "architext" ,
  31.                 "ask jeeves",
  32.                 "asterias2.0",
  33.                 "augurfind" ,
  34.                 "baiduspider" ,
  35.                 "bannana_bot" ,
  36.                 "bdcindexer" ,
  37.                 "crawler" ,
  38.                 "crawler@fast" ,
  39.                 "docomo" ,
  40.                 "fast-webcrawler",
  41.                 "fluffy the spider",
  42.                 "frooglebot",
  43.                 "geobot",
  44.                 "googlebot",
  45.                 "mediapartners-google",
  46.                 "gulliver",
  47.                 "henrythemiragorobot",
  48.                 "ia_archiver",
  49.                 "infoseek",
  50.                 "kit_fireball",
  51.                 "lachesis",
  52.                 "lycos_spider",
  53.                 "mantraagent",
  54.                 "mercator",
  55.                 "msnbot",
  56.                 "moget" ,
  57.                 "muscatferret" ,
  58.                 "nationaldirectory-webspider" ,
  59.                 "naverrobot" ,
  60.                 "ncsa beta" ,
  61.                 "netresearchserver" ,
  62.                 "ng/1.0" ,
  63.                 "osis-project" ,
  64.                 "polybot" ,
  65.                 "pompos" ,
  66.                 "scooter" ,
  67.                 "seventwentyfour" ,
  68.                 "sidewinder" ,
  69.                 "sleek spider" ,
  70.                 "slurp" ,
  71.                 "steeler" ,
  72.                 "szukacz" ,
  73.                 "t-h-u-n-d-e-r-s-t-o-n-e" ,
  74.                 "teoma" ,
  75.                 "turnitinbot" ,
  76.                 "ultraseek" ,
  77.                 "vagabondo" ,
  78.                 "voilabot" ,
  79.                 "w3c_validator" ,
  80.                 "zao" ,
  81.                 "zyborg"
  82.                 );

  83.                 $useragent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
  84.                 $isspider = 0;
  85.                 while( $pattern = array_pop( $spiders ))
  86.                 {
  87.                         $isspider = strpos( $useragent, $pattern);
  88.                         if( is_integer( $isspider ) )
  89.                         {
  90.                                 $isspider = $pattern;
  91.                                 break;
  92.                         }
  93.                 }
  94.         }
  95.         return $isspider;
  96. }



  97. /*
  98.         No gaurantees with this one,
  99.         Its supposed to sniff out smallscreen devices based on user agent
  100.         I found that there is no comprehensive list of useragents though

  101.         returns the useragent string if true
  102. */

  103. function is_smallscreen()
  104. {
  105.         global $_SERVER;
  106.         static $issmallscreen = 'init';

  107.         if( $issmallscreen != 'init' )
  108.         {
  109. // its set so don't do anything
  110.         } else {
  111. // don't know if this is a spider or not yet
  112.                 $smallscreens = array(
  113.                         'alcatel',
  114.                         'ericsson',
  115.                         'nokia',
  116.                         'panasonic',
  117.                         'phillips',
  118.                         'samsung',
  119.                         'sanyo',
  120.                         'wml',
  121.                         'mot',
  122.                         'wap',
  123. #                        'ppc',        # caused issues with Mac OS X
  124.                         'windows ce'
  125.                 );

  126.                 $useragent = strtolower( $_SERVER["HTTP_USER_AGENT"] );
  127.                 $issmallscreen = 0;       
  128.                 while( $pattern = array_pop( $smallscreens ))
  129.                 {
  130.                         $issmallscreen = strpos( $useragent, $pattern);
  131.                         if( is_integer( $issmallscreen ) )
  132.                         {
  133.                                 $issmallscreen = $pattern;
  134.                                 break;
  135.                         }
  136.                 }
  137.         }
  138.         return $issmallscreen;
  139. }

  140. ?>

复制代码
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

黑屋|存档|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2024-4-27 11:06 , Processed in 0.082544 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表