PHP update

I want to determine if the browser is updated or not PHP, and if the browser is updated, which PHP code should execute. Please help me. Any piece of code is very useful.

Thanks in advance,

Regards Naveen

+4
source share
6 answers

If the page has been refreshed, you expect that the two requests following each other will be for the same URL (path, file name, query string) and the same form content (if any) (POST data). This can be quite a lot of data, so it is best to use a hash. So...

<?php session_start(); //The second parameter on print_r returns the result to a variable rather than displaying it $RequestSignature = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].print_r($_POST, true)); if ($_SESSION['LastRequest'] == $RequestSignature) { echo 'This is a refresh.'; } else { echo 'This is a new request.'; $_SESSION['LastRequest'] = $RequestSignature; } 

In an AJAX situation, you need to be careful which files you insert into this code so as not to update the LastRequest signature for scripts called asynchronously.

+12
source

When the user clicks the refresh button, the browser includes an additional header that appears in the $ _SERVER array.

Check the refresh button using the following:

  $refreshButtonPressed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0'; 
+13
source
 <?php session_start(); if (!isset($_SESSION["visits"])) $_SESSION["visits"] = 0; $_SESSION["visits"] = $_SESSION["visits"] + 1; if ($_SESSION["visits"] > 1) { echo "You hit the refresh button!"; } else { echo "This is my site"; } // To clear out the visits session var: // unset($_SESSION["visits"]); ?> 
+3
source

If you mean that you want to distinguish when the user first comes to the page from the moment the page is reloaded, check the referrer. In php, this is: $ _SERVER ["HTTP_REFERER"]. See if it is equal on the page where your script is running. Perhaps the client does not provide this information, if this happens, you can set a cookie or session variable to keep track of what was the last page requested.

+1
source

If someone refreshes the page, the same request will be sent as the previous one. Therefore, you should check if the current request matches the last. This can be done as follows:

 session_start(); $pageRefreshed = false; if (isset($_SESSION['LAST_REQUEST']) && $_SERVER['REQUEST_URI'] === $_SESSION['LAST_REQUEST']['REQUEST_URI']) { if (isset($_SERVER['HTTP_REFERER'])) { // check if the last request's referrer is the same as the current $pageRefreshed = $_SERVER['HTTP_REFERER'] === $_SESSION['LAST_REQUEST']['HTTP_REFERER']; } else { // check if the last request didn't have a referrer either $pageRefreshed = $_SERVER['HTTP_REFERER'] === null; } } // set current request as "last request" $_SERVER['LAST_REQUEST'] = array( 'REQUEST_URI' => $_SERVER['REQUEST_URI'], 'HTTP_REFERER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null ); 

I have not tested it, but it should work.

+1
source

To prevent the form from being re-processed when the user clicks the refresh button of the browser or back, you need to use the session variable of the page instance identifier and the hidden form input containing this variable. when these two do not match, the user refreshed the page and you should not rework the form. for more information see:

https://www.spotlesswebdesign.com/blog.php?id=11

+1
source

All Articles