Page Refresh

How I redirect user if window refresh , I am using Php for that but it not working…

<?php
$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) &&($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'); 
if($pageRefreshed == 1){
    echo header ("location:  error-page.com") ;
}else{
    //Stay on same page
}
?>

Try adding this script after the if condition:

<?php 
// ADDED FULL SCRIPT
$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) &&($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'); 
if($pageRefreshed == 1){
    echo header ("location:  error-page.com") ;
}else{
    //Stay on same page
}

// cosmetic to add line feeds
echo '<pre>';
  print_r( $_SERVER );
echo '</pre>';
exit; // optional

edit:
Should “location: error-page.com” be"location: error-page.php"?

Could you write full script. I am confused.

Previous post amended with the “full script”.

The idea is to view the $_SERVER items and amend the test relocation script.

Using of server, I can seen on my screen about my browser details like: user agent, ip etc

Personally I think the following line is way to complicated and could be rewritten because the time taken to debug the script and to ensure it is doing what you want it to do is far in excess of this same script:

$pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) 
               && 
               (
                 $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' 
                 || 
                 $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'
               ); 
if($pageRef)
{
 ///
}else{
 ///
}

Looks like we are going on wrong path. Actually you script is not working end to end . What you want tell me sir, pls clear you code or mention /post in full script. I want if user refresh the page then he will redirect in error page else not.

Why are you programming without using error_reporting, etc.

Try adding this script to the top of your page and if “script is not working end to end” then copy and paste the errors and warnings.

<?php
  // THIS MUST BE THE FIRST LINE OTHERWISE ERRORS WILL BE REPORTED
  declare( strict_types = 1 ); // REMOVE IF NOT USING PHP 7
  ini_set( 'error_reporting', 'true');
  error_reporting( -1 ); // MAXIMUM ERROR REPORTING IS BEST

// your script goes here

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.