Usage
include("counter.php");
Counter(__FILE__);//Increase a count for the file
if($PHP_SELF=="/index.php")
{
$count=Counter("INDEX_COUNT");//Add a count to the home page
}
else
{
$count=Counter("INDEX_COUNT","" ,0);//Get homepage count
}
echo "You are the $count visitor";
?>
--------counter.php-- ---------
if(!isset($PHP_INCLUDE_COUNTER_PHP))
{$PHP_INCLUDE_COUNTER_PHP=__FILE;
$counter_error_state=0;
$counter_error_msg="";
function Counter($file,$query="",$add=1)
{
$db_name="database";
$db_user="username";
$db_pass="password";
$db_table="counter";
if(empty($file))
$counter_error_state=-10 0;
$counter_error_msg="The first parameter is missing or empty";
return -100; (empty($ db_user)||!$db_user||$db_user=="")$res=@mysql_connect("localhost");
else $res=@mysql_connect("localhost",$db_user,$db_pass); > turn -10;
}
if(!@ mysql_select_db($db_name))
return -11;
l_query("CREATE TABLE ".$db_table." (id INTEGER AUTO_INCREMENT,PRIMARY KEY (id),file VARCHAR(255),query VARCHAR(255),time VARCHAR(255),count INT)"))
$counter_error_states=-20;
$counter_error_msg ="Cannot create data table";
@mysql_free_result($db_res);
"" AND query="".$query. """;
if(!$db_res=@mysql_query($str))
> > return -30;
num>1)
"An unexpected error occurred = wrong number of data rows";
str="INSERT ";
$strWhere=" ";
if($num==1)
{
$row=@mysql_fetch_array($db_res);
@mysql_free_result($db_res); count"];
$id=$row["id"];
$str="UPDATE ";
$strWhere=" WHERE id= $id";
if($add<1)return $count;
$count+=$add;
$str.=$db_table." SET file="".$file."",query="".$query."",time="".date("Y;n;d;G;i;s")."",count=".$count.$strWhere;
$db_res=@mysql_query($str);
if(!$db_res)
{
$counter_error_states=-50;
$counter_error_msg="不能添加或更新记录";
return -50;
}
return $count;
}
}
}
?>