於是昨天便認真的做了一下,功能多一點,可以對各種搜索引擎統計分析。可以在多個時間段進行查看。其實代碼很簡單,為了更簡潔些,代碼壓縮到6k.分為6個文件
1.安裝程序 spilder_install.php
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>安裝插件</title>
</head>
<?php
if($_POST['act']=='install')//如果是安裝
{
$mysql_host=trim($_POST['mysql_host']);//獲取主機
$mysql_user=trim($_POST['mysql_user']);//獲取用戶名
$mysql_pwd=trim($_POST['mysql_pwd']);//獲取密碼
$mysql_db=trim($_POST['mysql_db']);//數據庫
$table_prefix=trim($_POST['table_prefix']);//獲取前綴
if($link=mysql_connect($mysql_host,$mysql_user,$mysql_pwd))
{
echo "連接服務器成功!.................<br>";
}
else
{
echo "<script>alert('鏈接出錯!請檢查數據庫服務器配置!');history.go(-1);</script>";
}
if(mysql_select_db($mysql_db,$link))
{
echo "連接數據庫成功!.............<br>正在創建表................<br>";
}
$sql="CREATE TABLE `spilder_sp_count` (
`id` bigint(20) NOT NULL auto_increment,
`r_time` int(11) NOT NULL,
`r_name` varchar(50) NOT NULL,
`r_url` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=4 ;
";
mysql_query("DROP TABLE IF EXISTS `".$table_prefix."sp_count`;");//刪除已存在
$sql=str_replace("spilder_",$table_prefix,$sql);//替換前綴
if(mysql_query($sql))
{
echo "表創建成功!正在寫入文件";
}
$f=file_get_contents("spilder_config.php");//獲取配置內容
$f=str_replace(" ","",$f);//去除空格
$f=preg_replace("/mysql_host=.*;/iUs","mysql_host='{$mysql_host}';",$f);
$f=preg_replace("/mysql_user=.*;/iUs","mysql_user='{$mysql_user}';",$f);
$f=preg_replace("/mysql_pwd=.*;/iUs","mysql_pwd='{$mysql_pwd}';",$f);
$f=preg_replace("/mysql_db=.*;/iUs","mysql_db='{$mysql_db}';",$f);
$f=preg_replace("/table_prefix=.*;/iUs","table_prefix='{$table_prefix}';",$f);
$f=preg_replace("/sp_admin=.*;/iUs","sp_admin='{$sp_admin}';",$f);
file_put_contents("spilder_config.php",$f);
file_put_contents("isinstall.txt","OK");
echo "恭喜!薦禮啦蜘蛛統計程序安裝成功 <a href=\"index.php\">查看</a>";
exit();
}
?>
<body>
<table width="600" border="0" align="center">
<tr>
<td align="center">薦禮啦 蜘蛛統計查看器</td>
</tr>
</table>
<form method="post">
<table width="600" border="0" align="center" cellspacing="0">
<tr>
<td width="280"> </td>
<td width="610">開始安裝</td>
</tr>
<tr>
<td align="right">服務器:</td>
<td><input name="mysql_host" type="text" id="mysql_host" value="localhost" />
(一般不用改)</td>
</tr>
<tr>
<td align="right">用戶名:</td>
<td><input type="text" name="mysql_user" id="mysql_user" /></td>
</tr>
<tr>
<td align="right">密碼:</td>
<td><input type="password" name="mysql_pwd" id="mysql_pwd" /></td>
</tr>
<tr>
<td align="right">數據庫:</td>
<td><input type="text" name="mysql_db" id="mysql_db" /></td>
</tr>
<tr>
<td align="right">表前綴:</td>
<td><input name="table_prefix" type="text" id="table_prefix" value="spilder_" />
<input name="act" type="hidden" id="act" value="install" /></td>
</tr>
<tr>
<td align="right">管理密碼:</td>
<td><input type="password" name="sp_admin" id="sp_admin" /></td>
</tr>
<tr>
<td align="right"> </td>
<td><input type="submit" name="button" id="button" value="安裝" /></td>
</tr>
</table>
</form>
</body>
</html>
2.蜘蛛記錄文件
復制代碼 代碼如下:
<?php
require("spilder_config.php");//調用配置文件
$link=mysql_connect($mysql_host,$mysql_user,$mysql_pwd) or die("服務器連接出錯");//鏈接數據庫
mysql_select_db($mysql_db,$link) or die('數據庫連接出錯');
$searchbot = get_naps_bot();
if($searchbot)
{
date_default_timezone_set('PRC'); //設置默認時區
$r_time=strtotime(date("Y-m-d H:i:s"));//最後更新時間
$ServerName = $_SERVER["SERVER_NAME"] ;
$ServerPort = $_SERVER["SERVER_PORT"] ;
$ScriptName = $_SERVER["SCRIPT_NAME"] ;
$QueryString = $_SERVER["QUERY_STRING"] ;
$url="http://".$ServerName ;
If ($ServerPort != "80")
{
$url = $url.":".$ServerPort ;
}
$url=$url.$ScriptName ;
If ($QueryString !="")
{
$url=$url."?".$QueryString ;
}
//$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$GLOBALS['db']->db_query("insert into {$table_prefix}sp_count(r_time,r_name,r_url) values('$r_time','$searchbot','$url')");
}
function get_naps_bot()
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($useragent, 'googlebot') !== false){
return 'Googlebot';
}elseif (strpos($useragent, 'msnbot') !== false){
return 'MSNbot';
}elseif (strpos($useragent, 'slurp') !== false){
return 'Yahoobot';
}elseif (strpos($useragent, 'baiduspider') !== false){
return 'Baiduspider';
}elseif (strpos($useragent, 'sogou') !== false){
return 'Sogoubot';
}elseif (strpos($useragent,'soso') !== false){
return 'Sosobot';
}elseif(strpos($useragent,'youdao')!==false)
{
return 'Youdao';
}
else
{
return false;
}
}
?>
3.蜘蛛統計查看文件
復制代碼 代碼如下:
<?php
session_start();
require("spilder_config.php");
if($_GET['act']=='logout')
{
$_SESSION['ss_sp_admin']='';//清空
}
if($_GET['sp_admin_login'])
{
if($_GET['sp_admin_login']==$sp_admin)
{
$_SESSION['ss_sp_admin']=$sp_admin;
}
}
date_default_timezone_set('PRC'); //設置默認時區
$day_start=strtotime(date("Ymd")."000001");//一天的開始時間
$day_out=strtotime(date("Ymd")."235959");//一天的結束時間
$day=3600*24;//一天的時間
$link=mysql_connect($mysql_host,$mysql_user,$mysql_pwd) or die("服務器連接出錯");
mysql_select_db($mysql_db,$link) or die('鏈接數據庫出錯');//連接數據庫
if($_GET['act']=='del')
{
if($_SESSION['ss_sp_admin']<>'')
{
if($_GET['dt']==0)//清空
{
mysql_query("delete from {$table_prefix}sp_count ");
}
$d_time=0;
if($_GET['dt']==1)//刪除30天前
{
$d_time=$day_out-$day*30;
}elseif($_GET['dt']==2)//刪除60天前
{
$d_time=$day_out-$day*60;
}elseif($_GET['dt']==3)//刪除90天前
{
$d_time=$day_out-$day*90;
}elseif($_GET['dt']==4)//刪除180天前
{
$d_time=$day_out-$day*180;
}elseif($_GET['dt']==5)//刪除360天前
{
$d_time=$day_out-$day*360;
}
if($d_time>0)
{
mysql_query("delete from {$table_prefix}sp_count where r_time<'$d_time'");//刪除數據
}
}
}
$ss=intval($_GET['ss']);
if($ss>0)//查看分類統計
{
if($ss==999)//清空分類
{
$_SESSION['ss_ss']='';
}else
{
$_SESSION['ss_ss']=$ss;
}
}
$ss=$_SESSION['ss_ss'];
if($ss==1)
{
$ss='Baiduspider';
}elseif($ss==2)
{
$ss='Googlebot';
}elseif($ss==3)
{
$ss="MSNbot";
}elseif($ss==4)
{
$ss="Yahoobot";
}elseif($ss==5)
{
$ss="Sogoubot";
}elseif($ss==6)
{
$ss="Sosobot";
}elseif($ss==7)
{
$ss=7;
}
$t=intval($_GET['t']);//查看時間分類統計
if($t==0)//時時情況
{
if(empty($_SESSION['ss_ss']))
{
$sql =" select * from {$table_prefix}sp_count order by id desc ";//最新情況
$sql2="select count(*) from {$table_prefix}sp_count ";
}else
{
$sql="select * from {$table_prefix}sp_count where r_name='$ss' order by id desc ";
$sql2="select count(*) from {$table_prefix}sp_count where r_name='$ss' ";
}
$rscount=mysql_query($sql2);
$rscount=@mysql_fetch_array($rscount);
$rscount=$rscount[0];//獲取總記錄數
$page=intval(trim($_GET['page']));//當前頁數
$pagesize=30;//每頁記錄數
$pagecount=ceil($rscount/$pagesize);//獲取總頁數
$pageurl="";
if($page<=1)
{
$page=1;
$pageurl.=" 首頁 上一頁";
}else
{
$pageurl .=" <a href='?page=1&ss=".$ss."'>首頁</a> <a href='?ss=".$ss."&page=".($page-1)."'>上一頁</a>";
}
if($page>=$pagecount)
{
$page=$pagecount;
$pageurl .=" 下一頁 尾頁";
}else
{
$pageurl .=" <a href='?ss=".$ss."&page=".($page+1)."'>下一頁</a> <a href='?ss=".$ss."&page=".$pagecount."'>尾頁</a>";
}
if($page<=1)
{
$page=1;
}
$start_rs=intval(($page-1)*$pagesize);//開始
$end_rs=intval($page*$pagesize);//結束
$sql .=" limit {$start_rs},{$end_rs} ";
}
if($t==1)//查看今天天統計
{
$s_time=$day_start;//開始時間
$e_time=$day_out;//結束時間
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name ";
}elseif($t==2)//查看昨天統計
{
$s_time=$day_start-$day;//開始
$e_time=$day_out-$day;//結束
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name ";
}elseif($t==3)//查看前天天的統計
{
$s_time=$day_start-$day*2;
$e_time=$day_out-$day*2;
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name";
}elseif($t==4)//查看七天的統計
{
$s_time=$day_start-$day*7;
$e_time=$day_out;
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name ";
}
elseif($t==5)//查看30天的
{
$s_time=$day_start-$day*30;
$e_time=$day_out;
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name ";
}elseif($t==6)//查看90天的統計
{
$s_time=$day_start-$day*90;
$e_time=$day_out;
$sql="select count(*) as ct,r_name from {$table_prefix}sp_count where r_time between '$s_time' and '$e_time' group by r_name ";
}
$res=mysql_query($sql);// or die("請先安裝程序<a href='spilder_install.php'>安裝</a>");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>顯示蜘蛛記錄情況</title>
<style type="text/css">
td{font-size:14px;}
a,a:link{text-decoration:none; color:#000; }
a:hover{color:#F00;}
</style>
</head>
<body>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-bottom:6px;">
<tr>
<td height="28" align="center" bgcolor="#66CC00"><h1><a href="http://www.jianlila.com" target="_blank">薦禮啦</a> 蜘蛛查看器</h1></td>
</tr>
</table>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="65" height="38" bgcolor="#FFFFFF">分類查看</td>
<td width="403" align="center" bgcolor="#FFFFFF">
<a href="?ss=999">所有的</a>
<a href="?ss=1">百度</a>
<a href="?ss=2">谷歌</a>
<a href="?ss=3">必應</a>
<a href="?ss=4">雅虎</a>
<a href="?ss=5">搜狗</a>
<a href="?ss=6">搜搜</a>
<a href="?ss=7">有道</a></td>
<td width="322" bgcolor="#FFFFFF">
<a href="?t=0">時時</a>
<a href="?t=1">今天</a>
<a href="?t=2">昨天</a>
<a href="?t=3">前天</a>
<a href="?t=4">七天</a>
<a href="?t=4">30天</a>
<a href="?t=5">90天</a>
</td>
</tr>
</table>
<?php
if($_SESSION['ss_sp_admin']==$sp_admin)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:12px; margin-bottom:12px;">
<tr>
<td width="208" align="right" bgcolor="#FFFFFF">管理 <a href="?act=logout">退出</a> </td>
<td width="585" align="center" bgcolor="#FFFFFF">
<a href="?act=del&dt=0" onclick="return confirm('確定清空')">清空</a>
<a href="?act=del&dt=1" onclick="return confirm('確定刪除?')">30天前</a>
<a href="?act=del&dt=2" onclick="return confirm('確定刪除?')">60天前</a>
<a href="?act=del&dt=3" onclick="return confirm('確定刪除?')">90天前</a>
<a href="?act=del&dt=4" onclick="return confirm('確定刪除?')">180天前</a>
<a href="?act=del&dt=5" onclick="return confirm('確定刪除?')">360天前</a></td>
</tr>
</table>
<?php
}
?>
<?php
if($t==0)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:6px;">
<tr>
<td width="133" align="center" bgcolor="#FFFFFF">蜘蛛</td>
<td width="217" align="center" bgcolor="#FFFFFF">時間</td>
<td width="440" align="center" bgcolor="#FFFFFF">地址</td>
</tr>
<?php
while($rs=mysql_fetch_array($res))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $rs['r_name'];?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo date("Y-m-d H:i:s",$rs['r_time']); ?></td>
<td align="center" bgcolor="#FFFFFF"><a href="<?php echo $rs['r_url'];?>" target="_blank"> <?php echo $rs['r_url'];?></a></td>
</tr>
<?php
}
?>
</table>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:20px; margin-bottom:20px;">
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $pageurl;?></td>
</tr>
</table>
<?php
}else{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:6px;">
<tr>
<td width="219" align="center" bgcolor="#FFFFFF">蜘蛛</td>
<td width="574" align="center" bgcolor="#FFFFFF">統計</td>
</tr>
<?php
while($rs=mysql_fetch_array($res))
{
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><?php echo $rs['r_name'] ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rs['ct'];?></td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" style="margin-top:6px;">
<tr>
<td align="center" bgcolor="#FFFFFF">友情鏈接:<a href="http://www.jianlila.com" target="_blank">薦禮啦</a> <a href="http://www.aiwobama.com" target="_blank">愛我爸媽</a> <a href="http://www.heikediguo.net">黑客帝國</a></td>
</tr>
</table>
</body>
</html>