is it possible php code detect page refresh (like javascript)?

Recommended Answers

All 3 Replies

store it in a cookie, session or database each time the page is ran:

<?php
$_SESSION['page1opens']++;
?>
Member Avatar for diafol

Do you mean can it detect if the same page is accessed as opposed to coming from somewhere else?

You could check $_SERVER['HTTP_REFERER'], but it's not totally reliable as it's only really triggered by link. F5 keypress or reload button on browser won't set it. Also, some 3rd party software can change it. So it's probably a deal breaker.

My own thoughts on this work around a session. This needs to be at the top of every page.
Alternatively, you could use a combination some of the other $_SERVER variables as opposed to __FILE__.

session_start();

if(isset($_SESSION['lastpage']) && $_SESSION['lastpage'] == __FILE__){
    echo "Either a refresh or same page as last page impression when re-entering site within session timeout period.";
}else{
    echo "New page";
}

$_SESSION['lastpage'] = __FILE__;

One caveat (well several really) - this will not discriminate between F5/reload button, link press NOR changes to querystring parameters NOR form post. Additionally, if you enter a page, then navigate away from the site and then return to the very same page within a certain time later, you may trigger the reload message. Similarly using the back button following navigate away.

You can check querystring parameters for changes if you want to be strict about this, with $_SERVER['QUERY_STRING'] - this will help discriminate with Apache rewrites too, which can route all pages through say the index.php file, but just prettify the querystring, e.g.

www.example.com/gcse/chemistry-1/ and www.example.com/gcse/chemistry-2/

May show as being the same page as the actual addresses may be:

www.example.com/index.php?course=gcse&module=chemistry-1 and www.example.com/index.php?course=gcse&module=chemistry-2 respectively

If you use a form to send data to the same page (not the best way to send data, but still), you can check this with $_SERVER['REQUEST_METHOD'].

Here's some simple code for checking. Please note IT IS NOT PRODUCTION READY - very, very rough - just to give an idea:

session_start();

if(isset($_SESSION['lastpage']) && $_SESSION['lastpage'] == __FILE__){
    if($_SERVER['QUERY_STRING'] != $_SESSION['querystring']){
        echo "Same page but querystring changed";
    }elseif($_SERVER['REQUEST_METHOD'] == "POST"){
        echo "This is in response to a form submission";
    }else{
        echo "Either a refresh or same page as last page when re-entering site within session timeout period.";
    }
}else{
    echo "New page";
}

$_SESSION['lastpage'] = __FILE__;
$_SESSION['querystring'] = $_SERVER['QUERY_STRING'];
print_r($_SERVER);
?>

<html>
<body>
<form method="POST">
    <input type="submit" value="go" name="subme" />
</form>
</body>
</html>

I haven't looked at javascript initiated refresh, but I'm assuming they will REQUEST_METHOD as GET. php header() takes GET as far as I could test.

Typically, somebody here will come up with a one-line solution, now that I've shown my backside to the world!! :)

Javascript can log a keystroke like F5 though.

commented: Also usefull for my situation. Thnx. +0

hai diafol, thanks for your guidance , thanks a lot , it is working .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.