I have folowing code in which i am using some conditions on page to be open or redirected to any other url but php method for calling a url is not working here please any one help me how to get out of this issue here is my php code
<?php
$good_domains = array("http://172.17.0.221:84/cp.aspx","http://172.17.0.221:84/cp.aspx");
if(!in_array($_SERVER['HTTP_REFERER'],$good_domains)){
echo "<script>alert(\"NO\");</script>";
Redirect('http://www.google.com.pk');
}
else{
echo "<script>alert(\"YES\");</script>";
//echo $_SERVER['HTTP_REFERER'];
//Redirect('http://www.shakarganj.com.pk');
$URL="http://www.google.com";
header ("Location: $URL");
}
?>
When i run the page the folowing error is generated.
Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\a\sfpl\MT.php:9) in C:\xampplite\htdocs\a\sfpl\MT.php on line 15
You can't do a header based redirect if you have already sent information to the browser, which your call to echo
will do. You must remove the calls to echo
, and also make sure that there is no whitespace in front of your first </php
tag.
Direct from the header()
help page here :
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called.
You echo
a script tag in both sides of the if
statment - you need to remove them on your redirect side
In addition to the answers above — if you use JS to display YES/NO alert, why did you use PHP for redirecting?
You can redirect user in JS:
alert("NO"); document.location.replace("http://google.co.uk");
// otherwise
alert("YES"); document.location.replace("http://google.com");
And if you want to have delay before user will be redirected, you can use setTimeout
function:
alert("NO");
// wait 5 seconds before redirect
setTimeout(5000, function()
{
document.location.replace("http://google.co.uk");
});
Since you have already done an echo
statement, PHP cannot send anymore headers to the output.
You have to make sure that any header()
calls are performed before any other output to your page.
that is a standard error; the header sends http headers and at the moment the output is started, the headers are already sent
in this case you can avoid this problem by using output buffering (check http://php.net/ob_start ); you need the option to be enabled on your server and to check your buffer size
At the top of your index file add this:
ob_start();
This will remove this warning if i'm not mistaken.
采用
echo '<META HTTP-EQUIV="refresh" content="0; URL=your_url">';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.