In a PHP file, when do I need to redirect the user, and the headers are already sent, so I can not use the php header function, in this case is the best way to redirect the user?
The fastest and most reliable method, regardless of brand of browser users?
echo '<script type="text/javascript">window.top.location="http://localhost/";</script>';
// OR
echo '<meta http-equiv="refresh" content="0;url=' .$location. '">';
UPDATE
Here is my end result code, which I am using now, if the headers have already been sent to where I cannot redirect to the main page, I just bring me the home page, so instead of including the body page, it will include my home page and footer
function validlogin($url) {
if (!isset($_SESSION['auto_id']) || ($_SESSION['auto_id']=='')) {
$_SESSION['sess_login_msg'] = 'Please login';
$_SESSION['backurl'] = $url;
$temp = '';
if (headers_sent() === false){
header("Location: /");
exit();
}else{
include 'mainbody.inc.php';
include 'footer.inc.php';
exit();
}
}
}
source
share