php Record Search Engine Crawling Record Implementation Code

  • 2021-09-12 00:34:03
  • OfStack

Here's the complete code:


// Record search engine crawling records  $searchbot = get_naps_bot(); 
if ($searchbot) 
{ $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); 
$url = $_SERVER['HTTP_REFERER']; 
$file = WEB_PATH.'robotslogs.txt'; 
$date = date('Y-m-d H:i:s'); 
$data = fopen($file,'a'); 
fwrite($data,"Time:$date robot:$searchbot URL:$tlc_thispage/r/n"); 
fclose($data);
}

WEB_PATH is the root directory path of define under index. PHP, which means that robotslogs. txt files are placed in the root directory.

Pass get_naps_bot() Get the spider crawl record, and then store the data in the variable $tlc_thispage under processing 1 through addslashes.

fopen Open the robotslogs. txt file, write the data through the function fwrite, and close it through the function fclose.

Because I don't think it is necessary, I deleted the code on my website, so there is no effect example.

PS: php the code to get the crawling records of each search spider

Support the following search engines: Baidu, Google, Bing, Yahoo, Soso, Sogou, Yodao crawling website records!

Code:


<?php 
/**
*  Get search engine crawl records 
* edit by www.ofstack.com
*/
function get_naps_bot() 
{ 
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']); 
if (strpos($useragent, 'googlebot') !== false){ 
return 'Google'; 
} 
if (strpos($useragent, 'baiduspider') !== false){ 
return 'Baidu'; 
} 
if (strpos($useragent, 'msnbot') !== false){ 
return 'Bing'; 
} 
if (strpos($useragent, 'slurp') !== false){ 
return 'Yahoo'; 
} 
if (strpos($useragent, 'sosospider') !== false){ 
return 'Soso'; 
} 
if (strpos($useragent, 'sogou spider') !== false){ 
return 'Sogou'; 
} 
if (strpos($useragent, 'yodaobot') !== false){ 
return 'Yodao'; 
} 
return false; 
} 
function nowtime(){ 
$date=date("Y-m-d.G:i:s"); 
return $date; 
} 
$searchbot = get_naps_bot(); 
if ($searchbot) { 
$tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); 
$url=$_SERVER['HTTP_REFERER']; 
$file="www.ofstack.com.txt"; 
$time=nowtime(); 
$data=fopen($file,"a"); 
fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n"); 
fclose($data); 
} 
?>

Summarize


Related articles: