I am working on a project and I am required to run my program on someone else\'s webserver. It is a pretty simple login page that I am having the problem with. The program
By using the below code we redirect the page
$page = $_SERVER['REQUEST_URI'];
echo '<script type="text/javascript">';
echo 'window.location.href="'.$page.'";';
echo '</script>';
echo "<script>window.location.href='yourPage.php'</script>";
You cannot set headers after outputting some text, so either you move the header
call before the echo
statements (fine in your case) or you use output buffering.
I use this function for redirect...
Which works in all situations..even if headers are already sent..or even javascript is disabled..
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
You probably have already sent content to the browser before the php header is sent. This can be just a \n after a closing php tag ( ?> )
To find the place where unwanted output is generated canbe hard on bigger projects or long lines of classes extending each other. To find the problem you can do this:
error_reporting(E_ALL);
ini_set("display_errors", 1);
header("Location: https://mydomain.com/myLoginAdress/");
die();
Strict error reporting will throw line and file of the output.
Redirecting via headers is illegal if any content has come through. Take out all the echo
s and it should work. If you do want there to be a message shown to the user before the redirect, you'll likely have to do it with JavaScript via location.href
.