Reputation: 1294
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
Upvotes: 2
Views: 510
Reputation: 1196
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");
});
Upvotes: 3
Reputation: 10303
At the top of your index file add this:
ob_start();
This will remove this warning if i'm not mistaken.
Upvotes: 2
Reputation: 5397
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
Upvotes: 2
Reputation: 2233
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.
Upvotes: 2
Reputation: 38147
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
Upvotes: 3
Reputation: 15560
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.
Upvotes: 5