Table of Contents
php防止恶意刷新与刷票的方法,php刷新刷票
Home php教程 php手册 php防止恶意刷新与刷票的方法,php刷新刷票

php防止恶意刷新与刷票的方法,php刷新刷票

Jun 13, 2016 am 09:20 AM
php refresh malicious prevent

php防止恶意刷新与刷票的方法,php刷新刷票

本文实例讲述了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 ']);
}


也可以利用COOKIE

复制代码 代码如下:

$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 代码:

复制代码 代码如下:

session_start();
?>



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles