前言:
之前分享过类似的代码,但是没有进行注释,并且部分代码有冗余,所以重新分享一下。以下是完整的代码:
// 记录搜索引擎爬行记录$
searchbot
= get_naps_bot
();
if
($searchbot
)
{ $tlc_thispage
= addslashes
($_SERVER
['HTTP_USER_AGENT
']); //首先读取 UA
并小写处理
$url
= $_SERVER
['HTTP_REFERER
']; //输出 URL
$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
是 index
.php
中定义的根目录路径,即 robotslogs
.txt
文件位于根目录下。
使用 get_naps_bot
()函数获取蜘蛛爬行记录,并使用 addslashes
()函数对数据进行处理,将结果存储在变量$tlc_thispage
中。
使用 fopen
()函数打开 robotslogs
.txt
文件,使用 fwrite
()函数将数据写入文件,最后使用 fclose
()函数关闭文件。
html
*/
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
.jb51
.net
.txt
";
$time
=nowtime
();
$data
=fopen
($file
,"a
");
fwrite
($data
,"Time
:$time
robot
:$searchbot
URL
:$tlc_thispage
\n
");
fclose
($data
);
}
?>
结语:
以上所述是汇站给大家介绍的 php
记录搜索引擎爬行记录,希望对大家有所帮助,如果大家有任何疑问请在文章下方留言,看到会及时回复大家的。
转载请注明:汇站网 » 实现搜索引擎爬行记录的 PHP
代码