To Check Page is Refreshed in PHP, we use PHP superglobal variable.
$_SERVER['HTTP_CACHE_CONTROL']
: Cache-control is an HTTP protocol header which specifies the policies of browser caching on both side, one is server-side and another is client-side.
These policies are used to check what information is cached and the maximum age of that information on the browser.
Source Code to Check Page is Refreshed in PHP
1 2 3 4 5 6 7 8 9 |
<?php $is_page_refreshed = (isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0'); if($is_page_refreshed ) { echo 'This Page Is refreshed.'; } else { echo 'This page is first time visited. Not refreshed.'; } ?> |
Copy and paste the complete code to the PHP file and run it, on the first run it says “This page is first time visited. Not refreshed.” and after that “This Page Is refreshed.”
Cache-Control: Max-Age
The Cache-Control: Max-Age defines the time in seconds to store the cached information on the browser side and also set the expiration time of the same.
Example:
Here is complete example and explanations How we Check Page is Refreshed in PHP.
To know more about Cache-control you check elaboration here: https://www.imperva.com/learn/performance/cache-control/
Also Check:
- Login with Google Account using PHP Step by Step
- How to Keep Value After Page Reload in PHP
- Save contact form data in CSV file using PHP
- 2 Ways to Open URL in New Tab Using JavaScript
- How to Embed PDF in Website Using HTML
Happy Coding..!
One Reply to “How to Check Page is Refreshed in PHP”