Example of php preventing websites from being refreshed by f5

Source: Internet
Author: User
Tags php file

The first two days of moving to Alibaba Cloud suffered a headache... Low host configuration, high demand for Nima wp...

It's just a few times before F5. Why are you so embarrassed? Why are there so many bad host configurations that have been used before...

O (???) O Alas, I will not speak out about Alibaba Cloud. Maybe when you see this site, this station is still squatting here on Alibaba Cloud!

A piece of code is attached below

Prevent the database from being tightened due to frequent refresh ....

Method 1

---------------------------------------

The code is as follows: Copy code

<? Php
Session_start ();
$ K = $ _ GET ['K'];
$ T = $ _ GET ['t'];
$ AllowTime = 1800; // Anti-refresh time
$ 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;
}     
?>


---------------------------------------
Method 2

---------------------------------------

The code is as follows: Copy code

<? Php
// Changed by www.111cn.net
Session_start ();
$ Allow_sep = "2 ";
If (isset ($ _ SESSION ["post_sep"])
{
If (time ()-$ _ SESSION ["post_sep"] <$ allow_sep)
{
Exit ("Please do not refresh frequently. Refresh again after 2 seconds of rest ");
}
Else
{
$ _ SESSION ["post_sep"] = time ();
}
}
Else
{
$ _ SESSION ["post_sep"] = time ();
}
?>


---------------------------------------
Method 3

---------------------------------------

The code is as follows: Copy code
<?
Session_start ();
If (! Empty ($ _ POST [name]) {
$ Data = $ _ POST [name];
$ Tag = $ _ POST [tag];
If ($ _ SESSION [status] ==$ tag ){
Echo $ data;
} Else {
Echo "refresh not allowed! ";
   }
}
$ V = mt_rand (1,10000 );
?>
<Form method = "post" name = "magic" action = "f5.php"> <input type = "hidden"
Name = "tag" value = "<? = $ V?> "> <Input type = text name =" name "> <input type =" submit "value =" submit ">
</Form>
<?
Echo $ v;
$ _ SESSION [status] = $ v;
?>


---------------------------------------

What are their advantages ?? You can guess.

Note !~~~~~~~~~~~~~~~~~~~~~~~

The code is ready, but some other operations are required to apply the code to WordPress.

Because the above code is based on session verification, if you refresh the page within 2 seconds, then he will execute the exit () function to output a message and exit the current script. As a result, the following content will not be loaded. Therefore, it is best to put this code in the header, first let the code be executed, and then load other things.

If you put the code in footer and the result is loaded on the entire page, only "Please do not refresh frequently" is output in the last line. The effect is better, to see the effect, press F5 twice.

Of course, the best practice is to create a php file and call it in the header.

There are two advantages:

One is convenient to modify the function code. You do not need to open the header file every time, or mistakenly modify the code elsewhere,

Second, if an error occurs, you can quickly modify and check the file, or even directly delete the file.

The code is as follows: Copy code


<? Php include ('regiondes/forbiddenCC. Php');?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">


Note the following:
Must be called at the beginning of all pages !!!

From: http://www.qinghuo.net/network/2981.html

 

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.