php实例之简单的网站浏览统计
2009-08-23 14:51
399 查看
最近在学php,跟着教程做了一个非常简单的网站流量统计的实例,拿出来跟大家分享。
counter.php代码:
<?php
//设置cookie的有效时间
setcookie("visit","visit",time()+900)
?>
<?php
//通过外部一个txt文本记录流量,用到的操作有文件操作函数与图片生产操作。
Header("Content_type:image/png");
if (file_exists("Counter.txt")) {
//从文件中取得计数值
$fp=fopen("Counter.txt","r");//打开文件
$Counter=fgets($fp,10);//获取counter的值
fclose($fp);//关闭文件
}
else
{
//如果文件不存在,创建一个新的文件,并指定计数器的值为0
$fp=fopen("Counter.txt","w");//以写的方式打开counter.txt文件
$Counter=0;//设置counter的值为0
}
//计数值累加,并写入计数器文件中
$fp=fopen("Counter.txt","w");//以写的方式打开文件
if (!isset($visit)) {
$Counter=$Counter+1;//计数加1
}
fwrite($fp,$Counter);
//fputs($fp,$Counter);//将值写入所在位标
fclose($fp);
//按8字符宽度格式化计数值
$Counter=sprintf("%08d",$Counter);
//创建image
$img=imagecreate(80,18);//创建一个面积为:80*18图片
$bule=imagecolorallocate($img,0,0,255);//为图片分配颜色
$red=imagecolorallocate($img,255,0,0);//为图片分配颜色
imagestring($img,5,5,1,$Counter,$red);//画出图片
imagepng($img);//以png的形式输出
imagedestroy($img);//销毁图片
?>
counter.html调用页面:
<html>
<head>
<title>Counter</title>
</head>
<body>
<center>您是本站第<br><img src="Counter.php"><br>位访问本地的客人!
</center>
</body>
</html>
counter.php代码:
<?php
//设置cookie的有效时间
setcookie("visit","visit",time()+900)
?>
<?php
//通过外部一个txt文本记录流量,用到的操作有文件操作函数与图片生产操作。
Header("Content_type:image/png");
if (file_exists("Counter.txt")) {
//从文件中取得计数值
$fp=fopen("Counter.txt","r");//打开文件
$Counter=fgets($fp,10);//获取counter的值
fclose($fp);//关闭文件
}
else
{
//如果文件不存在,创建一个新的文件,并指定计数器的值为0
$fp=fopen("Counter.txt","w");//以写的方式打开counter.txt文件
$Counter=0;//设置counter的值为0
}
//计数值累加,并写入计数器文件中
$fp=fopen("Counter.txt","w");//以写的方式打开文件
if (!isset($visit)) {
$Counter=$Counter+1;//计数加1
}
fwrite($fp,$Counter);
//fputs($fp,$Counter);//将值写入所在位标
fclose($fp);
//按8字符宽度格式化计数值
$Counter=sprintf("%08d",$Counter);
//创建image
$img=imagecreate(80,18);//创建一个面积为:80*18图片
$bule=imagecolorallocate($img,0,0,255);//为图片分配颜色
$red=imagecolorallocate($img,255,0,0);//为图片分配颜色
imagestring($img,5,5,1,$Counter,$red);//画出图片
imagepng($img);//以png的形式输出
imagedestroy($img);//销毁图片
?>
counter.html调用页面:
<html>
<head>
<title>Counter</title>
</head>
<body>
<center>您是本站第<br><img src="Counter.php"><br>位访问本地的客人!
</center>
</body>
</html>
相关文章推荐
- php ajax网站浏览统计功能的简单实现第1/2页
- php ajax网站浏览统计功能的简单实现第1/2页
- php统计网站/html页面浏览访问次数程序
- 简单实用的网站PHP缓存类实例
- [置顶] 网站浏览统计功能的简单实现-- vb2005xu自己动手系列(2)
- php统计网站/html页面浏览访问次数程序
- 简单实用的网站PHP缓存类实例
- php实例三之网站浏览量统计
- CI(CodeIgniter)简单统计访问人数实现方法[原创]_php实例_脚本之家
- php判断手机浏览还是web浏览,并执行相应的动作简单实例
- php教程统计静态html页面浏览访问次数代码
- php实现MVC框架的简单实例
- 最简单的PHP MVC留言本实例(二)
- PHP MySql增删改查的简单实例
- php生成静态页面的简单实例
- php实现简单的语法高亮函数实例分析
- php的ajax简单实例
- js综合应用实例简单的表格统计
- php 数组随机取值的简单实例
- PHP网站开发实例教程-知识点归纳1