How To Detect a page refresh or new page entered in PHP

Detect a page refresh or new page entered in PHP

Detect a page refresh or new page entered in PHP

To detect a whether a page is refresh or page is entrered i.e. accessed via some other page link clicked, I prefer to use $_SERVER. For me using this variable is a better choice rather than making loops and asigning extra variables, unless it is not needed. Loops and variable asignment may prove better in temporary visit counts.

What is $_SERVER?

$_SERVER is a array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. So there is no guarantee that every web server will provide any of these. Some servers may omit some, or provide others which are not provided by your server or mine.

How to detect a page refresh with PHP?

While browing we usually refresh the page using:

  • Pressing “F5” key
  • Right click and chosing “Reload”
  • Pressing a key combination of Ctrl+R
  • And many other options avalable in different platforms

Sometimes we have need to detect if the page was refreshed using “F5” key, or right click and “Reload” or any other method available. Either way, this can be detected somehow from server side. In this snippet, I will show you how to do this with PHP.

Leave a Reply