Php: Detect Page Refresh

PHP: Detect Page Refresh

i have solved the problem ... HURRAHHH with no session & no cookies

the solution is a combination of PHP : AJAX : JavaScript

the query that you want to run on Page Load & not on page Refresh run it as via AJAX call lets say my function for doing that is

function runQUERY()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","doIT.php",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
}

and i can simply check with Javascript that the page is a fresh load or its a refresh by doing the following

<head>
<script type="text/javascript">
function checkRefresh()
{
if( document.refreshForm.visited.value == "" )
{
// This is a fresh page load
alert ( 'Fresh Load' );
document.refreshForm.visited.value = "1";
..call you AJAX function here
}
else
{
// This is a page refresh
alert ( 'Page has been Refreshed, The AJAX call was not made');

}
}
</script>
</head>

<body onLoad="checkRefresh()">

<form name="refreshForm">
<input type="hidden" name="visited" value="" />
</form>

</body>
</html>

and in your doIT.php simple add your PHP code which you were going to put in the normal page

<?php
mysql_query("UPDATE---------");
//put any code here, i won't run on any page refresh
?>

Detect whether the browser is refreshed or not using PHP

If the page was refreshed then you'd expect two requests following each other to be for the same URL (path, filename, query string), and the same form content (if any) (POST data). This could be quite a lot of data, so it may be best to hash it. 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'd have to be careful about which files you put this code into so as not to update the LastRequest signature for scripts which were called asynchronously.

Refresh a page using PHP

You can do it with PHP:

header("Refresh:0");

It refreshes your current page, and if you need to redirect it to another page, use following:

header("Refresh:0; url=page2.php");

Detect browser refresh in Iphone using PHP

It looks like Safari doesn't send a Cache-Control header after the refresh (this can happen for several reasons). It is not a good idea to rely on the browser to notify you of an refresh with a header, as browsers may change over time.

A better solution to this problem is to use a session, since it will work regardless of the browser.

session_start(); //place it at the start of the PHP document, before anything except headers is send to browser

if (!isset($_SESSION["refresh"]))
$_SESSION["refresh"] = 0;
$_SESSION["refresh"] = $_SESSION["refresh"] + 1;

if ($_SESSION["refresh"] > 1)
{
//you refreshed the page!
}
else
{
//first time
}

EDIT: Other solutions to this problem are:

  • Using cookies (on server side). This won't work if user has disabled cookies.
  • Using javascript code on client side. Here it depends on the problem - the solution via javascript can sometimes be unnecessarily complicated, other times required.

Is there a way to differentiate between a page refresh and pushing a submit button?

Trying to determine of a page is being refreshed or not is not the way to resolve this issue. What you need to do is prevent the browser from ever resubmitting that same form submission.

The Post/Redirect/Get pattern resolves this issue. Basically, after the form has been processed you want to redirect the user to the page you wish them to see by using a HTTP 303 redirect. This tells the browser to replace the form page in the browser history making it impossible to resubmit the form.

Here's what it may look like in your code:

<input type="text" name="var2" required>
<input type="text" name="var3" required>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if($_POST && isset($_POST['Submit'])) {
$conn = mysqli_connect("localhost", "root","");
mysqli_select_db ($conn,'database');
$var1 =$_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
$sql = "INSERT into database (var1,var2,var3) VALUES ('$var1','$var2','$var3')";
if ($result = mysqli_query($conn, $sql)) {
header('Location: thankyou.php', true, 303);
exit;
}
}

FYI, please read about SQL injection. Instead of building queries with string concatenation, use prepared statements with bound parameters. See this page and this post for some good examples.



Related Topics



Leave a reply



Submit