Reputation: 81
How can I detect if user refreshed the page with PHP?
I tried $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
But it didn't work for me because i have no-cache.
Here's What worked for me:
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']);
if($pageWasRefreshed ) {
// Page was refreshed
}
else {
// Page wasn't refresh
}
Any water stones in this method?
Upvotes: 2
Views: 1568
Reputation: 31
You can do this multiple ways:
Set a session on the first time then check if the session is set or not.
Insert the client IP address in the MySQL database and check on every request if the user has visited before or not.
Now you have created your own logic and Google it how to implement this way I suggest you use 2nd solution.
Upvotes: 1
Reputation: 1569
Set a cookie
for the first time when someone visits the page. So, when refresh check whether cookie
exists or not, if yes the page is refreshed.
if (isset($_COOKIE['token']))
{
//The page is refeshed,
}
else
{
//first time user, set cookie.
setcookie('token');
}
Upvotes: 1