当前位置: 编程技术>php
本页文章导读:
▪PHP 函数执行效率的小比较
就是把原来的数组中的数都“拆”成“单”位的。 下面是自己写的一个函数: 代码如下: function splitStrToArray_mine($array) { $new_array = array(); foreach($array as $value) { $value = (string)$value; $len = strlen($.........
▪PHP类中Static方法效率测试代码
类的定义如下: 代码如下: class test { public static function a(){} public function b(){} } $obj = new test; 比较以下几种情况 test::a(); $obj->a(); $obj->b(); 测试代码: 代码如下: $obj = new test; $test_times = 10.........
▪发款php蜘蛛统计插件只要有mysql就可用
于是昨天便认真的做了一下,功能多一点,可以对各种搜索引擎统计分析。可以在多个时间段进行查看。其实代码很简单,为了更简洁些,代码压缩到6k.分为6个文件 1.安装程序 spilder_install..........
[1]PHP 函数执行效率的小比较
来源: 互联网 发布时间: 2013-11-30
就是把原来的数组中的数都“拆”成“单”位的。
下面是自己写的一个函数:
function splitStrToArray_mine($array)
{
$new_array = array();
foreach($array as $value)
{
$value = (string)$value;
$len = strlen($value);
for($i = 0; $i < $len; $i ++){
array_push($new_array, $value{$i});
}
}
return $new_array;
}
测试了一下,还是可以执行的,如下调用:
//测试数组
$data = array(12, 43, 87, 45, 98, 74, 83, 67, 12);
var_dump(splitStrToArray_mine($data));
输出结果为:
array(18) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "4"
[3]=>
string(1) "3"
[4]=>
string(1) "8"
[5]=>
string(1) "7"
[6]=>
string(1) "4"
[7]=>
string(1) "5"
[8]=>
string(1) "9"
[9]=>
string(1) "8"
[10]=>
string(1) "7"
[11]=>
string(1) "4"
[12]=>
string(1) "8"
[13]=>
string(1) "3"
[14]=>
string(1) "6"
[15]=>
string(1) "7"
[16]=>
string(1) "1"
[17]=>
string(1) "2"
}
虽然执行的不错,但是看看标准答案就会让你大吃一惊的,函数中就一句话,如下:
//标准函数
function splitStrToArray($array)
{
return str_split(implode("", $array));
}
于是写了脚本来测试自己的和标准的函数的运行效率差距,里面有一个 microtime_float() 函数用来提供精确时间的支持:
//测量时间的函数
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//自定义函数
function splitStrToArray_mine($array)
{
$new_array = array();
foreach($array as $value)
{
$value = (string)$value;
$len = strlen($value);
for($i = 0; $i < $len; $i ++){
array_push($new_array, $value{$i});
}
}
return $new_array;
}
//标准函数
function splitStrToArray($array)
{
return str_split(implode("", $array));
}
//测试数组
$data = array(12, 43, 87, 45, 98, 74, 83, 67, 12);
//开始测试
$mine_start = microtime_float();
splitStrToArray_mine($data);
$mine_end = microtime_float();
//标准函数调用
$sta_start = microtime_float();
splitStrToArray($data);
$sta_end = microtime_float();
echo "自己的函数调用运行时间为:" . (float)($mine_end - $mine_start) . " S <br />";
echo "标准的函数调用运行时间为:" . (float)($sta_end - $sta_start) . " S <br />";
$multiple = (int)((float)($mine_end - $mine_start) / (float)($sta_end - $sta_start));
echo "前者是后者的:" . $multiple . " 倍!";
来看看输出结果:
自己的函数调用运行时间为:9.3936920166E-005 S
标准的函数调用运行时间为:2.69412994385E-005 S
前者是后者的:3 倍!
多次刷新页面的话,可以发现标准函数的执行效率基本上是自己的函数的 3 倍!当然,标准的函数中使用了 PHP 的内置函数: str_split(),implode(),所以要比自己写函数快得多,对 str_split() 函数没有印象?来看看手册解释:
str_split -- Convert a string to an array(将一个字符串转换成数组)
函数描述:
array str_split ( string string [, int split_length] )
Converts a string to an array. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
例 1. Example uses of str_split()
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
Output may look like:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
下面是自己写的一个函数:
代码如下:
function splitStrToArray_mine($array)
{
$new_array = array();
foreach($array as $value)
{
$value = (string)$value;
$len = strlen($value);
for($i = 0; $i < $len; $i ++){
array_push($new_array, $value{$i});
}
}
return $new_array;
}
测试了一下,还是可以执行的,如下调用:
代码如下:
//测试数组
$data = array(12, 43, 87, 45, 98, 74, 83, 67, 12);
var_dump(splitStrToArray_mine($data));
输出结果为:
代码如下:
array(18) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "4"
[3]=>
string(1) "3"
[4]=>
string(1) "8"
[5]=>
string(1) "7"
[6]=>
string(1) "4"
[7]=>
string(1) "5"
[8]=>
string(1) "9"
[9]=>
string(1) "8"
[10]=>
string(1) "7"
[11]=>
string(1) "4"
[12]=>
string(1) "8"
[13]=>
string(1) "3"
[14]=>
string(1) "6"
[15]=>
string(1) "7"
[16]=>
string(1) "1"
[17]=>
string(1) "2"
}
虽然执行的不错,但是看看标准答案就会让你大吃一惊的,函数中就一句话,如下:
代码如下:
//标准函数
function splitStrToArray($array)
{
return str_split(implode("", $array));
}
于是写了脚本来测试自己的和标准的函数的运行效率差距,里面有一个 microtime_float() 函数用来提供精确时间的支持:
代码如下:
//测量时间的函数
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//自定义函数
function splitStrToArray_mine($array)
{
$new_array = array();
foreach($array as $value)
{
$value = (string)$value;
$len = strlen($value);
for($i = 0; $i < $len; $i ++){
array_push($new_array, $value{$i});
}
}
return $new_array;
}
//标准函数
function splitStrToArray($array)
{
return str_split(implode("", $array));
}
//测试数组
$data = array(12, 43, 87, 45, 98, 74, 83, 67, 12);
//开始测试
$mine_start = microtime_float();
splitStrToArray_mine($data);
$mine_end = microtime_float();
//标准函数调用
$sta_start = microtime_float();
splitStrToArray($data);
$sta_end = microtime_float();
echo "自己的函数调用运行时间为:" . (float)($mine_end - $mine_start) . " S <br />";
echo "标准的函数调用运行时间为:" . (float)($sta_end - $sta_start) . " S <br />";
$multiple = (int)((float)($mine_end - $mine_start) / (float)($sta_end - $sta_start));
echo "前者是后者的:" . $multiple . " 倍!";
来看看输出结果:
自己的函数调用运行时间为:9.3936920166E-005 S
标准的函数调用运行时间为:2.69412994385E-005 S
前者是后者的:3 倍!
多次刷新页面的话,可以发现标准函数的执行效率基本上是自己的函数的 3 倍!当然,标准的函数中使用了 PHP 的内置函数: str_split(),implode(),所以要比自己写函数快得多,对 str_split() 函数没有印象?来看看手册解释:
str_split -- Convert a string to an array(将一个字符串转换成数组)
函数描述:
array str_split ( string string [, int split_length] )
代码如下:
Converts a string to an array. If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
例 1. Example uses of str_split()
代码如下:
<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
Output may look like:
代码如下:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
[2]PHP类中Static方法效率测试代码
来源: 互联网 发布时间: 2013-11-30
类的定义如下:
class test
{
public static function a(){}
public function b(){}
}
$obj = new test;
比较以下几种情况
test::a();
$obj->a();
$obj->b();
测试代码:
$obj = new test;
$test_times = 100;
$times = 10000;
$effi1 = array();
$effi2 = array();
while ($test_times-- > 0)
{
$time1 = microtime(true);
for($i=0; $i<$times; $i++)
{
test::a();
}
$time2 = microtime(true);
for($i=0; $i<$times; $i++)
{
$obj->a();
}
$time3 = microtime(true);
for($i=0; $i<$times; $i++)
{
$obj->b();
}
$time4 = microtime(true);
$effi1[] = ($time3 - $time2) / ($time2 - $time1);
$effi2[] = ($time4 - $time3) / ($time3 - $time2);
}
echo avg($effi1),"\n",avg($effi2);
最后的avg是自定义的计算平均数的一个函数:
function avg($arr)
{
$result = 0;
foreach ($arr as $val)
{
$result += $val;
}
$result /= count($arr);
return $result;
}
程序输出结果:
PHP 5.2.14
view sourceprint?1 0.76490628848091
2 1.0699484376399
view sourceprint?1 PHP 5.3
view sourceprint?1 0.56919482299058<BR>1.1016495598611
重复执行N(N>10)次,都与这个结果相差不大,说明:
1、直接通过类名访问静态方法的效率是通过实例访问静态方法的76%,甚至用PHP5.3时只有56%
2、通过实例访问静态方法的效率是访问非静态成员方法效率的106,在5.3版本变成110%
3、假设PHP从5.2升级到5.3时通过类名访问静态方法的效率没有降低,那么通过实例访问函数的效率至少提高了35%。我没看过PHP源码,有研究过PHP源码的朋友希望能告诉我这个假设是否成立 (我想应该是成立的)
说明:以上测试基于windows 7和php.exe,5.2.14使用了apache2.2测试结果没有区别,考虑到php.exe和通过web访问所执行的PHP核心是一样的,所以5.3懒得改服务器配置了,结果应该一样。
代码如下:
class test
{
public static function a(){}
public function b(){}
}
$obj = new test;
比较以下几种情况
test::a();
$obj->a();
$obj->b();
测试代码:
代码如下:
$obj = new test;
$test_times = 100;
$times = 10000;
$effi1 = array();
$effi2 = array();
while ($test_times-- > 0)
{
$time1 = microtime(true);
for($i=0; $i<$times; $i++)
{
test::a();
}
$time2 = microtime(true);
for($i=0; $i<$times; $i++)
{
$obj->a();
}
$time3 = microtime(true);
for($i=0; $i<$times; $i++)
{
$obj->b();
}
$time4 = microtime(true);
$effi1[] = ($time3 - $time2) / ($time2 - $time1);
$effi2[] = ($time4 - $time3) / ($time3 - $time2);
}
echo avg($effi1),"\n",avg($effi2);
最后的avg是自定义的计算平均数的一个函数:
代码如下:
function avg($arr)
{
$result = 0;
foreach ($arr as $val)
{
$result += $val;
}
$result /= count($arr);
return $result;
}
程序输出结果:
代码如下:
PHP 5.2.14
view sourceprint?1 0.76490628848091
2 1.0699484376399
view sourceprint?1 PHP 5.3
view sourceprint?1 0.56919482299058<BR>1.1016495598611
重复执行N(N>10)次,都与这个结果相差不大,说明:
1、直接通过类名访问静态方法的效率是通过实例访问静态方法的76%,甚至用PHP5.3时只有56%
2、通过实例访问静态方法的效率是访问非静态成员方法效率的106,在5.3版本变成110%
3、假设PHP从5.2升级到5.3时通过类名访问静态方法的效率没有降低,那么通过实例访问函数的效率至少提高了35%。我没看过PHP源码,有研究过PHP源码的朋友希望能告诉我这个假设是否成立 (我想应该是成立的)
说明:以上测试基于windows 7和php.exe,5.2.14使用了apache2.2测试结果没有区别,考虑到php.exe和通过web访问所执行的PHP核心是一样的,所以5.3懒得改服务器配置了,结果应该一样。
[3]发款php蜘蛛统计插件只要有mysql就可用
来源: 互联网 发布时间: 2013-11-30
于是昨天便认真的做了一下,功能多一点,可以对各种搜索引擎统计分析。可以在多个时间段进行查看。其实代码很简单,为了更简洁些,代码压缩到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.html"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='/blog_article/page/1/amp;ss/.html".$ss."'>首页</a> <a href='/blog_article/ss/.html".$ss."&page=".($page-1)."'>上一页</a>";
}
if($page>=$pagecount)
{
$page=$pagecount;
$pageurl .=" 下一页 尾页";
}else
{
$pageurl .=" <a href='/blog_article/ss/.html".$ss."&page=".($page+1)."'>下一页</a> <a href='/blog_article/ss/.html".$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='/blog_article/spilder_install.html'>安装</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" >
<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="/blog_article/ss/999.html">所有的</a>
<a href="/blog_article/ss/1.html">百度</a>
<a href="/blog_article/ss/2.html">谷歌</a>
<a href="/blog_article/ss/3.html">必应</a>
<a href="/blog_article/ss/4.html">雅虎</a>
<a href="/blog_article/ss/5.html">搜狗</a>
<a href="/blog_article/ss/6.html">搜搜</a>
<a href="/blog_article/ss/7.html">有道</a></td>
<td width="322" bgcolor="#FFFFFF">
<a href="/blog_article/t/0.html">时时</a>
<a href="/blog_article/t/1.html">今天</a>
<a href="/blog_article/t/2.html">昨天</a>
<a href="/blog_article/t/3.html">前天</a>
<a href="/blog_article/t/4.html">七天</a>
<a href="/blog_article/t/4.html">30天</a>
<a href="/blog_article/t/5.html">90天</a>
</td>
</tr>
</table>
<?php
if($_SESSION['ss_sp_admin']==$sp_admin)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td width="208" align="right" bgcolor="#FFFFFF">管理 <a href="/blog_article/act/logout.html">退出</a> </td>
<td width="585" align="center" bgcolor="#FFFFFF">
<a href="/blog_article/act/del/amp;dt/0.html" onclick="return confirm('确定清空')">清空</a>
<a href="/blog_article/act/del/amp;dt/1.html" onclick="return confirm('确定删除?')">30天前</a>
<a href="/blog_article/act/del/amp;dt/2.html" onclick="return confirm('确定删除?')">60天前</a>
<a href="/blog_article/act/del/amp;dt/3.html" onclick="return confirm('确定删除?')">90天前</a>
<a href="/blog_article/act/del/amp;dt/4.html" onclick="return confirm('确定删除?')">180天前</a>
<a href="/blog_article/act/del/amp;dt/5.html" onclick="return confirm('确定删除?')">360天前</a></td>
</tr>
</table>
<?php
}
?>
<?php
if($t==0)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" >
<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="/blog_article/</php echo $rs[.html'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" >
<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" >
<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" >
<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>
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.html"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='/blog_article/page/1/amp;ss/.html".$ss."'>首页</a> <a href='/blog_article/ss/.html".$ss."&page=".($page-1)."'>上一页</a>";
}
if($page>=$pagecount)
{
$page=$pagecount;
$pageurl .=" 下一页 尾页";
}else
{
$pageurl .=" <a href='/blog_article/ss/.html".$ss."&page=".($page+1)."'>下一页</a> <a href='/blog_article/ss/.html".$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='/blog_article/spilder_install.html'>安装</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" >
<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="/blog_article/ss/999.html">所有的</a>
<a href="/blog_article/ss/1.html">百度</a>
<a href="/blog_article/ss/2.html">谷歌</a>
<a href="/blog_article/ss/3.html">必应</a>
<a href="/blog_article/ss/4.html">雅虎</a>
<a href="/blog_article/ss/5.html">搜狗</a>
<a href="/blog_article/ss/6.html">搜搜</a>
<a href="/blog_article/ss/7.html">有道</a></td>
<td width="322" bgcolor="#FFFFFF">
<a href="/blog_article/t/0.html">时时</a>
<a href="/blog_article/t/1.html">今天</a>
<a href="/blog_article/t/2.html">昨天</a>
<a href="/blog_article/t/3.html">前天</a>
<a href="/blog_article/t/4.html">七天</a>
<a href="/blog_article/t/4.html">30天</a>
<a href="/blog_article/t/5.html">90天</a>
</td>
</tr>
</table>
<?php
if($_SESSION['ss_sp_admin']==$sp_admin)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" >
<tr>
<td width="208" align="right" bgcolor="#FFFFFF">管理 <a href="/blog_article/act/logout.html">退出</a> </td>
<td width="585" align="center" bgcolor="#FFFFFF">
<a href="/blog_article/act/del/amp;dt/0.html" onclick="return confirm('确定清空')">清空</a>
<a href="/blog_article/act/del/amp;dt/1.html" onclick="return confirm('确定删除?')">30天前</a>
<a href="/blog_article/act/del/amp;dt/2.html" onclick="return confirm('确定删除?')">60天前</a>
<a href="/blog_article/act/del/amp;dt/3.html" onclick="return confirm('确定删除?')">90天前</a>
<a href="/blog_article/act/del/amp;dt/4.html" onclick="return confirm('确定删除?')">180天前</a>
<a href="/blog_article/act/del/amp;dt/5.html" onclick="return confirm('确定删除?')">360天前</a></td>
</tr>
</table>
<?php
}
?>
<?php
if($t==0)
{
?>
<table width="800" border="0" align="center" cellspacing="1" bgcolor="#CCCCCC" >
<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="/blog_article/</php echo $rs[.html'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" >
<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" >
<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" >
<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>
最新技术文章: