0

I am trying to check if the page was refreshed or no using PHP and on all the browsers.

This is what I tried to do:

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

However it doesn't work on Google Chrome.

I've also tried to set a SESSION - but how can I destroy this session so users can still visit the page but not refresh it?

I've also tried other several answers from PHP: Detect Page Refresh

Any help would be appreciated.

Thanks!

Mario
  • 1,374
  • 6
  • 22
  • 48
  • 3
    Perhaps irrelevant, but can I ask why? Perhaps you are approaching something the wrong way. I could be totally wrong and no offence intended in my presumption. Just seems a little odd as however users cause your system to invoke a request, it should just be handled appropriately. – James Aug 18 '18 at 00:10
  • @James - Well. I am using a link shortener and I want the users to come from the shortened link only - I've already done that by the `HTTP_REFERER` header. Now I want to prevent the visitors from refreshing too. – Mario Aug 18 '18 at 00:15
  • So if they come to your site in any way other than via the shorter URL you want them to not be able to use your site? Seems a bit unfair on you and them. An alternative idea would be to redirect all URLs to the shorter one. Like with htaccess. Then people will eventually bookmark and share the shorter one as that is what they are used to seeing. – James Aug 18 '18 at 00:25
  • @James - No, this is not unfair because this is to prevent hack attempts on 1 page only. This page requires to be referred from the shortened link. – Mario Aug 18 '18 at 00:27
  • Just to be clear, I meant unfair in a "bad user experience" way. Also, what happens when they come from the short link, don't refresh, but click another link on your site? I think you need to add more explanation as to what you are trying to achieve, but also a little bit of "why". It's hard to advise on potential solutions. – James Aug 18 '18 at 00:27
  • @James - for your 3rd reply: This will be on 1 page only. – Mario Aug 18 '18 at 00:28
  • 1
    Javascript might be a better choice to go here... https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript ... There is no reliable method off detecting a page reload in PHP... Because HTTP protocol is stateless. – Raymond Nijland Aug 18 '18 at 00:37
  • I started writing an answer with an idea, but I'm not sure what you want is possible in just PHP (just my opinion of course). If the referrer is valid, then it'll always be valid with a page refresh. So I'm not sure there's going to be a way to distinguish between a legit first visit to the page and so referrer is valid, and a page refresh as the referrer will still be valid. You could set something in persistence, like a cookie, but there's no way to know to unset it as you don't know if the referrer is legit from a first visit or page refresh – James Aug 18 '18 at 00:48
  • @James - Read my answer which I've posted. Do you think it is good? – Mario Aug 18 '18 at 00:49
  • 1
    Sure but you asked for PHP lol in Javascript it's a very different question entirely. You're sorted now tho. The only problem that remains is you have a question asking for this to be done in PHP only but an answer with the solution in Javascript only. There are many questions where it's done with JS (which would have made this one a dupe). That's why changing the scope of your question is not ideal :) – James Aug 18 '18 at 00:50
  • @James - I've asked if it is possible to do in PHP or no because I've failed to do it. But as `Raymond Nijland` suggested: I've tried with JavaScript and it worked. – Mario Aug 18 '18 at 00:51
  • As I said above this makes the question "awkward". I cannot close as a dupe for a javascript one as the question is asking with PHP, but in comments and your answer you decided to instead use JS which means it's not a good answer because the question asked for PHP. You kinda gave a bad answer on your own question based on your question's requirement of PHP :P – James Aug 18 '18 at 00:55
  • @James - Since it is possible to do with JavaScript, then let me add the `javascript` tag. :P – Mario Aug 18 '18 at 00:57

1 Answers1

0

Thanks to Raymond Nijland for suggesting to do it with JavaScript.

Also thanks for everyone who tried to help.

I did it as follows:

<?php
$RedirectURI = "http://example.com";
?>
<html>
<head>
<noscript>
<meta http-equiv="refresh" content="0; url=<?php echo $RedirectURI; ?>" />
</noscript>
<script>
if (!window.performance) {
    window.location = "<?php echo $RedirectURI; ?>";
}
if (performance.navigation.type == 1) {
    window.location = "<?php echo $RedirectURI; ?>";
}
</script>
</head>
</html>
Mario
  • 1,374
  • 6
  • 22
  • 48