NotionCommotion Posted December 7, 2014 Share Posted December 7, 2014 How can I detect if the page was refreshed? I thought the following worked, however, after much head scratching, discovered it does not work for a POST request using the Chrome browser. Thanks <?php $refresh=isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0'; echo(($refresh?'refresh':'not refresh').'<br>'); echo('$_GET<pre>'.print_r($_GET,1).'</pre>'); echo('$_POST<pre>'.print_r($_POST,1).'</pre>'); ?> <ul> <li><a href="refresh.php?id=1">one</a></li> <li><a href="refresh.php?id=2">two</a></li> <li><a href="refresh.php?id=3">three</a></li> </ul> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="text" value="hello"> <input type="submit" name="submit" value="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/ Share on other sites More sharing options...
WinstonLA Posted December 7, 2014 Share Posted December 7, 2014 I don't quite understand... Do you trying do F5 protection? Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498879 Share on other sites More sharing options...
NotionCommotion Posted December 7, 2014 Author Share Posted December 7, 2014 I don't quite understand... Do you trying do F5 protection? I have a class that uses sessions to remember the browsers immediate previous state. I use it to validate when JS is not available (no, I do not validate using JS, but POST the data using Ajax and if no errors, redirect client side to the next page). Upon a non-ajax post request, I validate data, and if an error, store the error and redirect the page back to the form page. Going to another page will clear the history as desired, but I don't wish to clear it if it is just refreshed with F5 or equal. Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498891 Share on other sites More sharing options...
NotionCommotion Posted December 7, 2014 Author Share Posted December 7, 2014 This seems to work if(($_SERVER['REQUEST_METHOD']==='POST') || !isset($_SERVER['HTTP_CACHE_CONTROL']) || $_SERVER['HTTP_CACHE_CONTROL'] !== 'max-age=0'){ //store } Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498893 Share on other sites More sharing options...
hansford Posted December 7, 2014 Share Posted December 7, 2014 When you load the page check for some cookie value. If it's not there, set some cookie value; If the page gets refreshed it will check for that cookie value again. If it is there, you know the page has been revisited/refreshed. You will have to delete the cookie once it is no longer needed. It will be reset every time they visit that page. Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498916 Share on other sites More sharing options...
NotionCommotion Posted December 8, 2014 Author Share Posted December 8, 2014 When you load the page check for some cookie value. If it's not there, set some cookie value; If the page gets refreshed it will check for that cookie value again. If it is there, you know the page has been revisited/refreshed. You will have to delete the cookie once it is no longer needed. It will be reset every time they visit that page. Set it to what? A serialized GET or something? I am not concerned whether they have ever been there, only that it was the last page. Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498920 Share on other sites More sharing options...
hansford Posted December 8, 2014 Share Posted December 8, 2014 I am not concerned whether they have ever been there, only that it was the last page. Well, you have sessions. You can store the last page name/url in a session variable. Whatever page they visit, this session variable gets updated. Then you will always know what the last page visited was. Quote Link to comment https://forums.phpfreaks.com/topic/292952-detect-whether-the-page-was-refreshed/#findComment-1498928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.