How to Detect a Browser Refresh with PHP

Posted on: August 13th, 2015 by BuildMyWeb No Comments

There has been many a time where I’ve needed to detect a user refreshing their browser within a web application. Here is a really simple solution that has worked for me in Firefox, Chrome, Safari, but of course not in good ol’ reliable (cough, cough) Internet Explorer. The bane of all developers since it’s inception. But enough IE bashing. Ha ha! I kid! There is never enough IE bashing. It’s the suck! Always has been a nightmare and likely always will be!

The suckiness of IE aside, here is the solution:


if( isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' )
{
    // our page has been refreshed
}

You’re welcome.

Tags:

Leave a Reply