php访问量统计

写一个简单的类来统计网页的访问量

建一个fwl.class.php文件复制下面内容进去

这个只是用来简单的统计下网页被访问了多少次,和当前用户一共访问了多少次

写成一个类,在需要显示访问量的地方调用就好

<?php 
header("content-type:text/html;charset=utf-8");
/**
 * @Author    XiaoFeng
 * @DateTime  2015-11-6
 * @Blog Link http://xfphp.cn
 */
 	class Fwl{
 			
 			public $clickcount=0;
 			public $str;
 			public $ip;
 			public function __construct(){
 				//设置时区
				date_default_timezone_set('PRC');
				//获取当前时间
 				$date=date('Y-m-d H:i:s');
 				//获取当前用户ip
 				$ip=$_SERVER['REMOTE_ADDR'];
 				//拼凑ip和时间 并写入到文件的数据:ip|2015-11-6 10:24:15
 				$write = $ip . '|' .$date;
 				$write = "\r\n" . $write;
 				//写入数据到文件
				file_put_contents('record.txt',$write,FILE_APPEND);
				//把获取的ip赋值给$ip
				$this->ip=$ip;
				//读取记录并赋值给$str,给其它方法调用
				$this->str =file_get_contents('record.txt');

 			}



 			public function zfwl(){
 				$str=$this->str;
				//以行区分当前文件有多少行
				$rows = explode("\r\n",$str);

				//获取已经访问过的用户的数量
				$count = count($rows);
				echo "当本站总访问量{$count}次";

 			}
 			public function fwjc(){
 				//统计当前用户是第几次访问
 				$str=$this->str;
 				$ip=$this->ip;
 				$clickcount=$this->clickcount;
				//以行区分当前文件有多少行
				$rows = explode("\r\n",$str);
				foreach($rows as $value){
				//value代表一个访问记录
					$ips = explode("|",$value);

				//判读是不是当前用户查看的
					if($ips[0] == $ip){
				//以前访问的记录与当前用户的ip相同
					$clickcount++;
					}
				}
			echo "您是第{$clickcount}次来访问该网页<br/>";
 			}
 	}

?>

类写好了,再来说说调用,例如在index.php页面里调用

<?php 
header("content-type:text/html;charset=utf-8");
/**
 * @Author    XiaoFeng
 * @DateTime  2015-11-6
 * @Blog Link http://xfphp.cn
 */
include 'fwl.class.php'; //加载类文件
$w=new Fwl;              //实例化类
$w->zfwl();				 //调用输出访问总次数
$w->fwjc();				 //调用输出当前用户共访问了几次


?>


打 赏

小风博客 - XiaoFeng Blog - 佘佳栋的个人博客
请点评论按钮,登录后发表评论
  • 最新评论
  • 总共0条评论