Forum Moderators: coopster

Message Too Old, No Replies

redirect scripts and header ("Location: ...")

         

Trisha

1:01 am on Mar 5, 2004 (gmt 0)

10+ Year Member



I made a little redirect script but am getting a 'headers already sent' error on the line with the 'header ("Location: [someotherdomain.xx")'...] command. It doesn't make any sense to me why that is happening, since it should send the user to a different domain. Of course the headers are already sent for the page myredirectscript.php, but not for the page on the other domain.

What would cause this?

ahmed

7:40 am on Mar 5, 2004 (gmt 0)

10+ Year Member



make sure your script doesn't have any output before you use the header command. This means no HTML and no print or echo statements, and be careful of whitespace outside your PHP codeblock, that's also a no-no.

If you have to output something before using header(), look into the output buffering functions in the php manual.

Trisha

4:28 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



I don't have any print or echo statements, or any HTML, or white space.

I do have an if/else and an includes though. I wouldn't have thought those would cause any problems though. Maybe I have done something else fundamentally wrong. This is how I have tried to do it:


if (!$var_from_previous_page) {
header("Location: http.../default.html");
break;}
else{
include ("../functions.php");
$conn=dbconnect();
$rurl_sql="select F5 from db_table
where F4 = '$var_from_previous_page'";
$rurl_result = mysql_query($rurl_sql, $conn);
$rurl = mysql_fetch_array($rurl_result);
header("Location: $detail[F5]");
break;}

coopster

4:48 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You should actually use an exit [php.net] after a header [php.net]. break [php.net] simply ends execution of the current control structure. Also, you are using an array called $detail...is that really what you want there? Or did you mean to use the $rurl array?
header("Location: $rurl[F5]"); 
/* Make sure that code below does not get executed when we redirect. */
exit;

Trisha

5:09 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Thanks coopster, I didn't know about the break/exit thing and I did make a mistake with the 'detail/rurl' part.

The error I'm still getting though is this:

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/xx/functions.php:2) in /hsphere/local/home/xx/mydomain.com/redirect.php on line 12

Line 12 is: header("Location: $rurl[F5]");

I don't understand where that is coming from.

ahmed

5:18 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



try this :
at the very start of your script, before any includes, or any statements *at all*, type this :

ob_start();

and then on the line just before your call to header() type this :

$output = ob_get_clean();

and as coopster said, add an exit after the header.

If the redirect works after that, then something in between ob_start() and ob_get_clean() is outputting to the screen and by moving ob_start() down through the code block, you should be able to nail it down.

Grizzly

5:29 pm on Mar 5, 2004 (gmt 0)



Check your functions.php. Warning say output started in it (check line 2). May be you have some echo, print, etc. code in functions.php outside of any function.

Trisha

5:38 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



Thank you Grizzly and everyone for being so patient! The problem was with the functions.php file! I didn't read the error carefully enough! There was one line of space at the beginning of that file! I didn't think about that file being a problem because I use it everywhere - but not in a situation involving headers except here.

I promise I will start reading my errors more carefully for now on!

ahmed - I will remember the ob_start(); thing, for the next time I have problems.

Thanks again!