表名: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"); $[email protected]_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"); $[email protected]_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; } $[email protected]_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"."|"; $[email protected]_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<=$length;$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); ?>