php防止恶意刷新与刷票实现代码
恶意刷新就是不停的去刷新提交页面,导致大量无效数据了,下面我们来总结一下php 防止恶意刷新页面方法总结防止恶意刷页面的原理是要求在页面间传递一个验证字符串,在生成页面的时候 随机产生一个字符串,做为一个必须参数在所有连接中传递,同时将这个字符串保存在session中.
点连接或者表单进入页面后,判断session中的验证码是不是与用户提交的相同,如果相同,则处理,不相同则认为是重复刷新,在处理完成后将重新生成一个验证码,用于新页面的生成,代码如下:
<?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;
}
?>ie6提交两次我也碰到过,大致是用图片代替submit时,图片上有个submit(),这样会提交两次,如果只是submit钮我没碰到过提交两次的情况, 现在整理一下:
方法基本上前面几位说得差不多,接收的页即2.php分为两部分,一部分处理提交过来的变量,一部分显示页面,处理变量完毕用header( "location: ".$_SERVER[ 'PHP_SELF '])跳转到自身页本部分要做判断,如果没有post的变量就跳过,当然也可以跳到别的页面.
跳到别的页面返回时会有问题,建议做在一个php文件里,如果上页穿过来得变量不符合要求可以强制返回 <script> history.go(-1); </script>
只说了一下大体思路,也许高手们不会遇到此类问题,可是并不是每个人都是高手,代码如下:
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>也可以利用COOKIE,代码如下:
<?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);
}
?>session
主页面文件 index.php 代码如下:
<?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>counter.txt 文件为同目录下的记录登录数文件……
$counter=fgets($fp,1024);为读取文件中数值型值的方法,可包含小数点数值.
本文地址:http://www.phprm.com/develop/fs4637.html
转载随意,但请附上文章地址:-)