tags:

views:

1150

answers:

6

Hi,

I want to detect whether the browser is refreshed or not using PHP, and if the browser is refreshed, what particular PHP code should execute. Please help me. Any code snippet is very helpful.

Thanks in advance,

regards Naveen

A: 

What do you mean 'browser is refreshed'?

Logan Serman
i mean if user click on the browser refresh button
naveen
Ask questions in the comments of the question, don't post this as an answer.
TravisO
A: 

If you mean that you want to distinguish between when a user first comes to the page from when they reload the page check the referrer. In php it is: $_SERVER["HTTP_REFERER"]. See if it is equal the page your script is running on. It may be the case that the client doesn't provide this information, if that happens you could set a cookie or session variable to track what the last requested page was.

apphacker
+5  A: 
<?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"]);

?>
nlaq
This of course doesn't guarantee that they hit the refresh button. They might have navigated back to this page through (for example) one of your site's internal links.
Wickethewok
+4  A: 

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.

Bell
A: 

If someone refreshes a page, the same request will be sent as the previous one. So you should check whether the current request is the same as the last one. 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['REQEUST_URI'],
    'HTTP_REFERER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
);

I haven’t tested it but it should work.

Gumbo