Is there any way to identify whether someone has refreshed the current page using PHP rather than arriving from elsewhere.
I have access only to the code for the current page so I can't pass anything from the page elsewhere to check against.
Is there any way to identify whether someone has refreshed the current page using PHP rather than arriving from elsewhere.
I have access only to the code for the current page so I can't pass anything from the page elsewhere to check against.
The title of your question suggests that you're specifically looking for a way to detect when a page is refreshed using the F5 key. If that's the case, you'll definitely need JavaScript to detect if the key was pressed. If you don't care how the page was refreshed, then this answer should help: https://stackoverflow.com/a/456915/1144176. It takes into account the full URL as well as any form content.
Yes, there is a way of detecting the F5 Browser Refresh. I've teste this solution in Chrome and Firefox, it works.
$cacheControlRequestHeader = ', '.strtolower($_SERVER['HTTP_CACHE_CONTROL']).', ';
if (strpos($cacheControlRequestHeader, ', max-age=0, ') !== false){
$isF5Refresh = true;
}
When I tested it in Internet Explorer 11, "max-age" did not work. But you can detect Ctrl+F5 in IE with this code:
$cacheControlRequestHeader = ', '.strtolower($_SERVER['HTTP_CACHE_CONTROL']).', ';
if (strpos($cacheControlRequestHeader, ', no-cache, ') !== false){
$isControlF5RefreshIE = true;
}
Add an unload()
event which either sets a cookie, or starts a PHP session via AJAX. Then when the page is next loaded, check for the cookie or the session - if it's present and very recent, the user has just refreshed.
No, there is not.
PHP provides the $_SERVER
variable with the HTTP_REFERER
variable. Unfortunately, web-browsers only update the referer value (which they send to the webserver in a request) on location changes like clicking links etc; one side sending you to another (even if the page sends you to itself via a link). For F5 however, they do not (as the page is not its referer after all. The user took action by pressing f5).
You can, however, use JavaScript or other client-side scripts to track keypresses or the unloading of the page. You can then either use client-side scripts to identify the refresh on the client side, or update some user-tracking on the server side.
You could also track the users requested pages on the server side alone. This will not specifically identify F5 refreshs though. tab close/open, reopen link etc will also result in the same URI being requested.
There is no bullet-proof way to identify a F5 refresh on client or server side. But I have this idea:
Every time your page is loaded:
LAST_VIEWED_PAGE