PHP Judge visit is a search engine spider or ordinary user code summary _php instance

Source: Internet
Author: User

1, a recommended method: PHP to determine the search engine spider crawler or human access code, excerpted from Discuz x3.2

<?php
function Checkrobot ($useragent = ') {
	static $kw = Array (' bot ', ' crawl ', ' spider ', ' slurp ', ') Sohu-search ', ' Lycos ', ' Robozilla ');
	static $kw _browsers = Array (' MSIE ', ' Netscape ', ' opera ', ' Konqueror ', ' Mozilla ');

	$useragent = Strtolower (Empty ($useragent) $_server[' http_user_agent ': $useragent);
	if (Strpos ($useragent, ' http://') = = False && Dstrpos ($useragent, $kw _browsers)) return false;
	if (Dstrpos ($useragent, $kw _spiders)) return true;
	return false;
}
function Dstrpos ($string, $arr, $returnvalue = False) {
	if (empty ($string)) return false;
	foreach ((array) $arr as $v) {
		if (Strpos ($string, $v)!== false) {
			$return = $returnvalue? $v: true;
			return $return;
		}
	return false;
}
if (Checkrobot ()) {
	echo ' robotic crawler ';
} else{
	echo ' man ';
>

The actual application can be judged in this way, directly is not a search engine to perform operations

<?php
if (!checkrobot ()) {
//do something
}
?>

2, the second method:

Using PHP to achieve spider access log statistics

$useragent = Addslashes (Strtolower ($_server[' http_user_agent '));
 if (Strpos ($useragent, ' Googlebot ')!== false) {$bot = ' Google ';}
 ElseIf (Strpos ($useragent, ' Mediapartners-google ')!== false) {$bot = ' Google Adsense ';}
 ElseIf (Strpos ($useragent, ' Baiduspider ')!== false) {$bot = ' Baidu ';}
 ElseIf (Strpos ($useragent, ' Sogou spider ')!== false) {$bot = ' Sogou ';}
 ElseIf (Strpos ($useragent, ' Sogou web ')!== false) {$bot = ' Sogou web ';}
 ElseIf (Strpos ($useragent, ' Sosospider ')!== false) {$bot = ' SOSO ';}
 ElseIf (Strpos ($useragent, ' 360spider ')!== false) {$bot = ' 360Spider ';}
 ElseIf (Strpos ($useragent, ' Yahoo ')!== false) {$bot = ' Yahoo ';}
 ElseIf (Strpos ($useragent, ' MSN ')!== false) {$bot = ' MSN ';}
 ElseIf (Strpos ($useragent, ' MSNBot ')!== false) {$bot = ' msnbot ';}
 ElseIf (Strpos ($useragent, ' Sohu ')!== false) {$bot = ' Sohu ';}
 ElseIf (Strpos ($useragent, ' Yodaobot ')!== false) {$bot = ' Yodao ';}
 ElseIf (Strpos ($useragent, ' Twiceler ')!== false) {$bot = ' twiceler ';} ElseIf (Strpos ($useragent, ' Ia_archivEr ')!== false) {$bot = ' alexa_ ';}
 ElseIf (Strpos ($useragent, ' Iaarchiver ')!== false) {$bot = ' Alexa ';}
 ElseIf (Strpos ($useragent, ' slurp ')!== false) {$bot = ' Yahoo ';}
 ElseIf (Strpos ($useragent, ' bot ')!== false) {$bot = ' other Spider ';}
   if (Isset ($bot)) {$fp = @fopen (' Bot.txt ', ' a '); Fwrite ($fp, date (' y-m-d h:i:s '). " \ t ". $_server[" REMOTE_ADDR "]." \ t ". $bot." \ t ". ' http://'. $_server[' server_name '].$_server[' Request_uri ']. "
   \ r \ n ");
 Fclose ($FP); }

The third method:

We can judge by Http_user_agent to whether is the spider, the search engine's spider has its own unique mark, the following column takes a part.

function Is_crawler () { 
  $userAgent = strtolower ($_server[' http_user_agent ')); 
  $spiders = Array ( 
    ' Googlebot ',///Google crawler ' 
    baiduspider ',///Baidu crawler ' 
    Yahoo! Slurp ',///Yahoo crawler 
    ' Yodaobot ',// Youdao crawler 
    ' msnbot '//Bing crawler 
    //More reptile keywords 
  ); 
  foreach ($spiders as $spider) { 
    $spider = Strtolower ($spider); 
    if (Strpos ($userAgent, $spider)!== false) {return 
      true; 
    } 
  } 
  return false; 
}

The following PHP code comes with more spider logos

function Iscrawler () {echo $agent = Strtolower ($_server[' http_user_agent ')); 
            if (!empty ($agent)) {$spiderSite = Array ("Tencenttraveler", "baiduspider+"), "Baidugame", "Googlebot", "MSNBot", "sosospider+", "Sogou Web Spide 
            R "," Ia_archiver "," Yahoo! Slurp "," Youdaobot "," Yahoo slurp ", "MSNBot", "Java (often spam bot)", "Baiduspider", "voila", "Yand 
            Ex bot "," Bspider "," Twiceler "," Sogou Spider "," Speedy Spider ", 
            "Google AdSense", "Heritrix", "Python-urllib", "Alexa (IA archiver)", 
            "Ask", "Exabot", "Custo", "Outfoxbot/yodaobot", "YaCy", "Surveybot", "Legs", "LWP-trivial "," Nutch "," Stackrambler "," The Web Archive (IA archiver) "," 
            Perl tool "," Mj12bot "," Netcraft "," Msiecrawler "," WGet tools ", 
        "Larbin", "Fish Search",); 
            foreach ($spiderSite as $val) {$str = Strtolower ($val); 
            if (Strpos ($agent, $STR)!== false) {return true; 
    }} else {return false; } if (Iscrawler ()) {echo "Hello, Spider-Man!" 
"; else{echo "You are not spider essence!" 
";  }

Fourth method:

<?php $flag = false;
$tmp = $_server[' http_user_agent ']; if (Strpos ($tmp, ' Googlebot ')!== false) {$flag = true;} else if (Strpos ($tmp, ' Baiduspider ') >0) {$flag = true;} E LSE if (Strpos ($tmp, ' Yahoo! Slurp ')!== false) {$flag = true;} else if (Strpos ($tmp, ' MSNBot ')!== false) {$flag = Tru
E else if (Strpos ($tmp, ' Sosospider ')!== false) {$flag = true;} else if (Strpos ($tmp, ' Yodaobot ')!== false | | Strpos ($t MP, ' Outfoxbot ')!== false {$flag = true;} else if (Strpos ($tmp, ' Sogou web Spider ')!== false | | Strpos ($tmp, ' Sogou O Rion spider ')!== false) {$flag = true;} else if (Strpos ($tmp, ' Fast-webcrawler ')!== false) {$flag = true;} else if ( Strpos ($tmp, ' Gaisbot ')!== false) {$flag = true;} else if (Strpos ($tmp, ' Ia_archiver ')!== false) {$flag = true;} El Se if (strpos ($tmp, ' AltaVista ')!== false) {$flag = true;} else if (Strpos ($tmp, ' Lycos_spider ')!== false) {$flag = t
Rue else if (Strpos ($tmp, ' Inktomi slurp ')!== false) {$flag = true;} if ($flaG = = False) {header ("location:http://www.jb51.net". $_server[' Request_uri ')); Automatically go to http://www.jb51.net corresponding Page//$_server[' Request_uri ' for the path following the domain name//or replace header ("location:http://www.jb51.net/abc/d
  . php ");
Exit (); }?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.