<?php
session_start();
$k=$_GET[‘k‘];
$t=$_GET[‘t‘];
$allowTime = 1800;//防刷新时间
$ip = get_client_ip();
$allowT = md5($ip.$k.$t);
if(!isset($_SESSION[$allowT]))
{
$refresh = true;
$_SESSION[$allowT] = time();
}elseif(time() - $_SESSION[$allowT]>$allowTime){
$refresh = true;
$_SESSION[$allowT] = time();
}else{
$refresh = false;
}
?>
if(isset($_POST))
{
if(变量不符合要求)
<script> history.go(-1); </script>
else
//操作数据
...
if(操作完成)
header( "location: ".$_SERVER[ ‘PHP_SELF ‘]);
}
<script language= "JavaScript ">
<!--
javascript:window.history.forward(1);
//-->
</script>
<?php
$c_file="counter.txt"; //文件名赋值给变量
if(!file_exists($c_file)) //如果文件不存在的操作
{
$myfile=fopen($c_file,"w"); //创建文件
fwrite($myfile,"0"); //置入“0”
fclose($myfile); //关闭文件
}
$t_num=file($c_file); //把文件内容读入变量
if($_COOKIE["date"]!="date(Y年m月d日)") //判断COOKIE内容与当前日期是否一致
{
$t_num[0]++; //原始数据自增1
$myfile=fopen($c_file,"w"); //写入方式打开文件
fwrite($myfile,$t_num[0]); //写入新数值
fclose($myfile); //关闭文件
//重新将当前日期写入COOKIE并设定COOKIE的有效期为24小时
setcookie("date","date(Y年m月d日)",time()+60*60*24);
}
?>
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通过session禁止页面刷新</title>
<style type="text/css">
<!--
.style1 {
font-size: 14px;
font-family: "华文仿宋";
font-weight: bold;
}
.style2 {font-family: "华文琥珀"}
-->
</style>
</head>
<body>
<?php
//使用文本存储数据
if($_SESSION[temp]==""){
if(($fp=fopen("counter.txt","r"))==false){
echo "打开文件失败!";
}else{ $counter=fgets($fp,1024); //读取文件中数据
fclose($fp); //关闭文本文件
$counter++; //计数器增加1
$fp=fopen("counter.txt","w"); //以写的方式打开文本文件
fputs($fp,$counter); //将新的统计数据增加1
fclose($fp); } //关闭文
//从文本文件中读取统计数据
if(($fp=fopen("counter.txt","r"))==false){
echo "打开文件失败!";
}else{
$counter=fgets($fp,1024);
fclose($fp);
echo "数字计数器: " .$counter ;
} //输出访问次数
$_SESSION[temp]=1; //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1
}else{
echo "<script>alert(‘您不可以刷新本页!!‘); history.back();</script>";
}
?>
<table width="300" border="0" cellpadding="0" cellspacing="0" background="images/141.jpg">
<tr>
<td height="35" align="center"><span class="style1">通过session禁止页面刷新</span></td>
</tr>
<tr>
<td height="40" align="center"><span class="style2">
<?php if(($fp=fopen("counter.txt","r"))==false){
echo "打开文件失败!";
}else{
$counter=fgets($fp,1024);
fclose($fp);
echo "网页访问量: " .$counter ;
} //输出访问次数
?>
</span></td>
</tr>
<tr>
<td height="25" align="center"> </td>
</tr>
</table>
</body>
</html>
参考来源:
php防止恶意刷新与刷票的方法
http://www.lai18.com/content/369326.html
《PHP安全编程系列》系列技术文章整理收藏
PHP安全编程系列收藏夹收藏了有关PHP安全编程方面的知识,对PHP安全编程提供学习参考
14PHP安全之以Apache模式安装时可能遇到的攻击及解决办法
15PHP安全之以CGI 模式安装时可能遇到的攻击及解决办法
50PHP安全编程:cookie暴露导致session被劫持
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/u011252402/article/details/47415101