PHP using session implementation to prevent page repeat refresh _php skills

Source: Internet
Author: User

How to prevent the page from recurring refresh, in the PHP environment can use the session to easily achieve.

B.php's Code

<?php//can only post-access if ($_server[' request_method '] = = ' Get ') {header (' http/1.1 404 Not Found '); Die (' Pro, page does not exist ');} 
Session_Start (); 
$fs 1=$_post[' a ']; 
$fs 2=$_post[' B ']; 
Anti-refresh time, the unit is seconds $allowTime = 30; 
Read the guest IP so that you can get the real IP start for IP limit refresh//if (! function_exists (' GetIP ')) {function getip () {Static $IP = NULL; 
if ($ip!== NULL) {return $ip; } if (Isset ($_server)) {if (Isset ($_server[' http_x_forwarded_for ')) {$arr = explode (', ', $_server[' Http_x_forwarde 
D_for ']); /* Fetch the X-unknown valid IP characters in x-forwarded-for? 
*/foreach ($arr as $XIP) {$xip = Trim ($XIP); 
if ($xip!= ' unknown ') {$ip = $XIP; 
Break 
}} elseif (Isset ($_server[' http_client_ip ')) {$ip = $_server[' http_client_ip ']; 
else {if (Isset ($_server[' remote_addr ')) {$ip = $_server[' remote_addr ']; 
else {$ip = ' 0.0.0.0 '; 
}} else {if (getenv (' http_x_forwarded_for ')) {$ip = getenv (' http_x_forwarded_for '); } elseif (getenv (' http_client_ip ')) {$ip = getenv (' Http_client_IP '); 
else {$ip = getenv (' remote_addr '); } preg_match ("/[\d\.] 
{7,15}/", $ip, $ONLINEIP); $IP =! Empty ($onlineip [0])? 
$ONLINEIP [0]: ' 0.0.0.0 '; 
return $IP; 
}/* Get real IP end/$reip = GetIP (); 
Correlation parameter MD5 Encryption $allowT = MD5 ($REIP. $FS 1. $fs 2); 
if (!isset ($_session[$allowT])) {$_session[$allowT] = time (); 
else if (time ()-$_session[$allowT]--> $allowTime) {$_session[$allowT] = time (); 
//If the refresh is too fast, give the 404header header directly and prompt else {header (' http/1.1 404 Not Found '); Die (' from '. $ip. '), you refresh too fast ');}

 ?>

The code is very simple, nothing more than the IP, as well as the post way to the need to prevent the refresh of the page data after MD5 encryption into the session, and then through the stored session to determine the refresh time interval to determine whether to allow refreshing. What is required is "$fs 1=$_post[' a"]; "," $fs 1=$_post[' a '); " Two parameters refer to other pages that are submitted by post to the required anti-refresh page. The reason why these parameters are added in addition to IP is to distinguish between different post results. (In fact, the so-called anti-refresh is to prevent a page from being repeatedly submitted.) )

More specifically, for example, the above code is placed at the beginning of the b.php page, and we have a form on the a.html page that reads:

Code:

<! doctype> 
 
 

You can see that the A and B 2 parameters submitted by this page are exactly the 2 parameters in the preceding b.php (in fact, it should be, in turn, determined by the parameters of the submitting page). In the previous PHP code, has been determined only through post access to the data submitted by the page, so the direct input address will get a 404-head error page, only through the Post method to get the page, while the post refresh will bring their own parameter address, This will enable the same page for each IP to prevent the refresh effect.

In addition, we can increase the post page through the Referer to determine the source of the site, to prevent cross-site submissions, but Referer can forge, and Firefox and IE8 often inexplicably referer lost situation, so temporarily do not add this code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.