Php methods to prevent websites from being refreshed, php methods to prevent refreshing summaries _ PHP Tutorial

Source: Internet
Author: User
Php prevents websites from being refreshed and php prevents refreshing. Php methods to prevent websites from being refreshed. php methods to prevent websites from being refreshed. examples in this article describe php methods to prevent websites from being refreshed. Share it with you for your reference. The specific method is as follows: for php to prevent websites from being refreshed, php to prevent refreshing summary

This article describes how php can prevent websites from being refreshed. Share it with you for your reference. The specific method is as follows:

For websites constructed using WP, frequent refresh may cause database tightening. The following is a piece of code to prevent the crash caused by frequent refresh.

Method 1: The code is as follows:

The code is as follows:

<? 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:

The code is as follows:

<? Php
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:

The code is as follows:

<? Php
Session_start ();
If (! Emptyempty ($ _ POST [name]) {
$ Data = $ _ POST [name];
$ Tag = $ _ POST [tag];
If ($ _ SESSION [status] ==$ tag ){
Echo $ data;
} Else {
Echo "refresh not allowed! ";
}
}
$ V = mt_rand (1,10000 );
?>

<? Php
Echo $ v;
$ _ SESSION [status] = $ v;
?>

Note: The code is ready, but some other operations are required to be applied 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 in other places. The other is that, if an error occurs, you can quickly modify and check the code, you can even directly delete an object. the code is as follows:

The code is as follows:

<? Php
Include ('regiondes/forbiddenCC. php ');
?>


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

I hope this article will help you with PHP programming.

Examples in this article describes how php can prevent websites from being refreshed. Share it with you for your reference. The specific method is as follows:...

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.