I have a few error pages that users are automatically redirected to when a specific error message needs to be shown to the user. Each error page does an auto redirect back to the previous page after a few seconds and each error page also contains a link the user can click on if the auto redirect does not work.
I'm using $SERVER['HTTP_REFERER'] to get the headers for the redirect from the error page back to the previous page. The problem I have is sometimes there is no referer header so the redirect and link from the error page fail. Sometime I even get a PHP "Cannot modify header information - headers already sent by (output started at..." error which I guess is caused by one of the PHP script includes already using $SERVER['HTTP_REFERER'] .
Below is the code I have at the top of each error page for the auto redirect:
<?php
if(isset($_SERVER['HTTP_REFERER'])){
$ref = $_SERVER['HTTP_REFERER'];
header( 'refresh: 4; url='.$ref);
}
?>
Below is the code I use for my links:
<a style="color:#0000FF;" href="<?php if(isset($_SERVER['HTTP_REFERER'])){ echo $_SERVER['HTTP_REFERER'];} ?>">Click if your browser does not redirect in 4 seconds.</a>
Below is a mock up of what I think will work, but I can't get the code right:
get $_SERVER['HTTP_REFERER'] info
if $_SERVER['HTTP_REFERER'] empty, then use http://www.mysite.com
send referer if not already sent by other script
Can someone help me figure out the coding🙂