表名:tpCounter(table of pages counter) 字段: 名称 类型 意义 id Int (10) auto_increment 序列号 pagename varchar(20) 页面标识,缺省为页面文件名 count Int(10) 计数值
表名:tiCounter(table of ip counter) 字段: 名称 类型 意义 id Int(10) auto_increment 序列号 ip varchar(20) Ip标识 count Int(10) 该ip访问次数 date datetime 最近访问时间 pages text 曾访问过的页面id,用’|’分隔
//网页计数,返回页面访问次数,整型 //$flag是是否增加计数的标志,缺省true function page_count($connect,$page_name,$flag=true){ $ip = getenv("REMOTE_ADDR"); $query=@mysql_query("select id,count from tpcounter where pagename='$page_name'",$connect) or die("invalid page query!"); if (!(mysql_num_rows($query))){ mysql_query("insert into tpcounter (pagename,count) values('$page_name',1)",$connect)or die("insert page failed"); $pidquery=@mysql_query("select id from tpcounter where pagename='$page_name'",$connect) or die ("select page id failed"); $pidarray=mysql_fetch_array($pidquery); $pid=$pidarray[id]; $return_num=1; }else { $array=mysql_fetch_array($query); $num=$array[count]; $pid=$array[id]; if ($flag) $num++; mysql_query("update tpcounter set count=$num where pagename='$page_name'",$connect)or die("update page failed"); $return_num=$num; } $pquery=@mysql_query("select pages from ticounter where ip='$ip'",$connect) or die ("invalid pages query!"); if (($flag)&&(mysql_num_rows($pquery))){ $parray=mysql_fetch_array($pquery); $ps="$parray[pages]"; $pstr="$parray[pages]"."$pid"."|"; mysql_query("update ticounter set pages='$pstr' where ip='$ip'",$connect)or die ("update ip failed"); } return $return_num; }
//ip计数,返回ip访问次数,整型 //功能除了计数还有时间更新 //$flag是是否增加计数的标志,缺省true //注意:ip_count的调用必须在page_count之前!!! function ip_count($connect){
$ip = getenv("REMOTE_ADDR");
$visit_time=date("Y:m:d:H:i"); $visit_pages="||"."$visit_time"."|"; $ipquery=@mysql_query("select count,pages from ticounter where ip='$ip'",$connect) or die ("invalid ip query!");
if (!(mysql_num_rows($ipquery))){//新的ip $pageStr="|"."$visit_pages"; mysql_query("insert into ticounter (ip,count,date,pages) values ('$ip',1,'$visit_time','$pageStr')",$connect)or die("insert ip failed"); return 1; }else{ //旧的ip $parray=mysql_fetch_array($ipquery); $ipnum=$parray[count]; $pageStr="$parray[pages]"."$visit_pages"; $ipnum++; mysql_query("update ticounter set count=$ipnum,date='$visit_time',pages='$pageStr' where ip='$ip'",$connect)or die("update ip failed"); return $ipnum; }
}
//网站计数,返回整型,网站访问次数 function site_count($connect){ if (!check_session()){ //session不存在 $ipnum=ip_count($connect); $num=page_count($connect,"website",true); }else{ //session存在 $num=page_count($connect,"website",false); } return $num; }
function displayCount($num){ $fileurl="countpng.php?count=".$num; return $fileurl; }
//显示计数值,type为显示类型,length为显示的长度,缺省6 //type=1图形形式 //type=2文本形式(缺省) function show_site_count($num,$length=6,$type=2){
$outStr=strval($num); for ($i=strlen($outStr)+1;$i $outStr="0"."$outStr"; } switch ($type){ case 1: echo " echo displayCount($outStr); echo "\">"; break; case 2: default: echo "$outStr"; } } ?>
if (!isset($inc))exit; $connect=mysql_connect('localhost','root','');//connect to server mysql_select_db("damio",$connect); //select database ,database name is damio
$sitecount=site_count($connect); if (isset($page_name)) page_count($connect,$page_name); ?>