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.
source share