2

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 

7 Answers 7

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Its Ok But I have to use the echo also and after the message in echo i want to redirect how this may possible ?
You want to display the message to the user, and then redirect? You will have to use a client side solution for the redirect in that case, either JavaScript or a meta tag. @balkon_smoke's answer provides a JS solution.
@user1220667 whats the point of echo-ing something then redirecting ????? do it client side if you want to show a message location.href = <url> in JavaScript
if he wants to do it server side he can make use of ob_Start() and ob_end_flush() functions. php.net/ob_start and php.net/ob_end_flush The way this answer is written isn't exactly telling him that there is a solution to his problem, just telling him more information on what is causing the warning message.
3

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

Comments

3

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"); }); 

Comments

2

At the top of your index file add this:

ob_start(); 

This will remove this warning if i'm not mistaken.

Comments

2

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.

Comments

2

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

Comments

0

use

echo '<META HTTP-EQUIV="refresh" content="0; URL=your_url">'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.