Home  >  Article  >  Backend Development  >  Summary of PHP methods to prevent the website from being refreshed, PHP method to prevent the website from being refreshed_PHP tutorial

Summary of PHP methods to prevent the website from being refreshed, PHP method to prevent the website from being refreshed_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:30731browse

A summary of ways to prevent the website from being refreshed in php, a summary of how to prevent the website from being refreshed in php

The example in this article describes how PHP prevents the website from being refreshed. Share it with everyone for your reference. The specific method is as follows:

For sites built with WP, frequent refreshes will cause database strain. Attached below is a piece of code to prevent crashes caused by frequent refreshes.

Method 1, the code is as follows:

Copy code The code is as follows:
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 The code is as follows:
session_start();
$allow_sep = "2";
if (isset($_SESSION["post_sep"]))
{
if (time() - $_SESSION["post_sep"] < $allow_sep)
{
exit("Please do not refresh frequently, rest for 2 seconds before refreshing");
}
else
{
$_SESSION["post_sep"] = time();
}
}
else
{
$_SESSION["post_sep"] = time();
}
?>

Method three, the code is as follows:
Copy code The code is as follows:
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);
?>
name="tag" value="">

echo $v;
$_SESSION[status] = $v;
?>

Note: The code is there, but there are some other operations required to apply it to WordPress.

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

If you put the code in the footer, the entire page is loaded and only the last line outputs "Please do not refresh frequently." If you put it in the header, the effect is better. If you want to see the effect, press F5 twice.

Of course the best way is to create a new php file and then call it in the header.

There are two benefits of doing this:

Firstly, it is convenient to modify the function code. You do not need to open the header file every time, and you are not afraid of accidentally changing the code in other places. Secondly, once an error occurs, you can quickly modify and check it, or even delete the file directly. The code is as follows:

Copy code The code is as follows:
include('includes/forbiddenCC.php');
?>

Please note here that must be called at the front of all pages.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920625.htmlTechArticleA summary of PHP’s methods to prevent the website from being refreshed, PHP’s method to prevent the website from being refreshed. An example of this article tells the PHP method to prevent the website from being refreshed. . Share it with everyone for your reference. The specific method is as follows: For...
Statement:
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