In PHP, we sometimes write simple website page visit counters. Below, I will introduce to you how to use PHP to implement counter code. I hope this method will be helpful to everyone.
Let’s add a counter to the homepage. Useful for demonstrating how to read and write files and create your own functions. counter.inc contains the following code:
The code is as follows
代码如下 |
复制代码 |
/*
|| 一个简单的计数器
*/
function get_hitcount($counter_file)
{
/* 将计数器归零
这样如果计数器还未被使用,初始值将是1
你当然也可以把初始值设成20000来骗人咯
*/
$count=0;
// 如果存放计数器文件已经存在,读取其中的内容
if ( file_exists($counter_file) )
{
$fp=fopen($counter_file,"r");
// 我们只取了前20位,希望你的站点不要太受欢迎啊
$count=0 fgets($fp,20);
// 由于函数fgets()返回字符串,我们可以通过加0的方法将其自动转换为整数
fclose($fp);
// 对文件操作完毕
}
// 增加一次计数值
$count ;
// 将新的计数值写入文件
$fp=fopen($counter_file,"w");
fputs($fp,$count);
fclose($fp);
# 返回计数值
return ($count);
}
?>
然后我们更改front.php3文件以显示这个计数器:
include("include/counter.inc");
// 我把计数值放在文件counter.txt中,读出并输出
printf (" d n",
get_hitcount("counter.txt"));
include("include/footer.inc");
?>
|
|
Copy code
|
代码如下 |
复制代码 |
1)文本计数器
$countfile="/count.txt"; //设置保存数据的文件
if (!file_exists($countfile)){//判断文件是否存在
exec( "echo 0 > $countfile");
}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
PRint "访问量总计:"."$num"."人次"; //显示访问次数
?>
2)图形计数器
$countfile="/count-num.txt"; //设置保存数据的文件
if (!file_exists($countfile)) //判断文件是否存在
{exec( "echo 0 > $countfile");}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
$len_str = strlen($num);
for($i=0;$i<$len_str;$i++){
$each_num = substr($num,$i,1);
$out_str = $out_str . "";
}
print "访问量总计:"."$out_str"."人次"; //显示访问次数
|
/*
|| A simple counter
*/
function get_hitcount($counter_file)
{
/* Reset counter to zero
This way if the counter has not been used yet, the initial value will be 1
Of course you can also set the initial value to 20,000 to deceive people
*/
$count=0;
// If the file storing the counter already exists, read its contents
if ( file_exists($counter_file) )
{
$fp=fopen($counter_file,"r");
// We only took the top 20, I hope your site will not be too popular
$count=0 fgets($fp,20);
// Since the function fgets() returns a string, we can automatically convert it to an integer by adding 0
fclose($fp);
//Complete file operation
}
//increment the count value
$count;
//Write new count value to file
$fp=fopen($counter_file,"w");
fputs($fp,$count);
fclose($fp);
# Return the count value
return ($count);
}
?> |
Then we change the front.php3 file to display this counter:
include("include/counter.inc");
// I put the count value in the file counter.txt, read it out and output it
printf ("
d n",
get_hitcount("counter.txt"));
include("include/footer.inc");
?>
Example 2
The code is as follows
|
Copy code
1) Text counter
$countfile="/count.txt"; //Set the file to save data
if (!file_exists($countfile)){//Determine whether the file exists
exec( "echo 0 > $countfile");
}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
PRint "Total visits:"."$num"."Persons"; //Display the number of visits
?>
2) Graphic counter
$countfile="/count-num.txt"; //Set the file to save data
if (!file_exists($countfile)) //Determine whether the file exists
{exec( "echo 0 > $countfile");}
$fp = fopen($countfile,"rw");
$length=filesize($countfile);
$num = fgets($fp,$length);
$num += 1;
exec( "rm -rf $countfile");
exec( "echo $num > $countfile");
$len_str = strlen($num);
for($i=0;$i<$len_str;$i++){
$each_num = substr($num,$i,1);
$out_str = $out_str . "";
}
print "Total visits:"."$out_str"."Persons"; //Display the number of visits
http://www.bkjia.com/PHPjc/632702.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632702.htmlTechArticleIn php, we sometimes write simple website page visit counters ourselves. The editor will introduce it to you below. Use PHP to implement counter code. I hope this method will be helpful to everyone. Let's...
|
|