Sesseion变量屏蔽刷新页面对计数器的影响

一 代码

<?php
	header("Content-Type:text/html;charset=utf-8");
	session_start();
?>
<link type="text/css" href="index.css" rel="stylesheet">
<div>
<?php
	if($_SESSION[time]==$_POST[time]){//检测页面中$_SESSION[time]是否与$_POST[time]相同
		$visit=file_get_contents("index");//取得文本文件信息
		$visit=$visit+1;//将值自动加1
		echo "<font>本网站已被访问<b>".$visit.$_SESSION[time]."</b>次</font>";//输出提示信息
		file_put_contents("index",$visit);//将加1后的值重新写入文件
	}else{
		$visit=file_get_contents("index");//获取到文本文件的值
		echo "<font>本网站已被访问<b>".$visit."</b>次</font>";//输出信息
	}
	$time=microtime();//获取当前时刻时间戳的微秒数
	$_SESSION[time]=$time;//将此微秒数保存在session中
?>	
<span>网站计数器</span>
	<form action="" method="post">
		<input type="hidden" name="time" value="<?php echo $time;?>">
	</form>
</div>

 

二 运行结果
第一次登陆,计数器加1,当再刷新页面(F5)时,计数器不再加1


 

猜你喜欢

转载自cakin24.iteye.com/blog/2374730