Forum Moderators: coopster

Message Too Old, No Replies

php cookie redirect

stupid headers!

         

ahmedtheking

4:36 pm on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a problem!

I have two documents, index.php and main.php.

within index.php, you have to choice out of 3 options (industry_pro, family or student). these options are the vari's carried into main.php (eg main.php?user=student) to set a certain theme and type of site specific to that person.

I wrote a little cookie script that would set a cookie for the vari of the user and then redirect the user once they come back!

in the index.php script, i have done a simple if 'isset' cookie function. here it is:

// find the cookie, if its set, redirect
if (isset($_COOKIE["user"])) {

// redirect
header ('Location: main.php?user=$user');

}

on the main.php pages, i have written a little script, like the one about, to write a cookie if one doesnt exist.

// if cookie isnt there, set it
if (isset($_COOKIE["user"])) { }
else {
// set the cookie
setcookie("user","$user", time() + 604800);
}

however, every time i test the script, i get this error in the main.php file:

Cannot mod header info - headers already sent by (output started at /home/firestar/public_html/roa/main.php:7) in /home/firestar/public_html/roa/main.php on line 13

bsterz

5:03 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



Use output buffering. Place the following line at the top of your script.

ob_start();

[us2.php.net...]

ahmedtheking

8:18 pm on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no, that didnt work!

dreamcatcher

12:24 am on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The setcookie function, like the header function cannot be called after input has already been sent to the browser.

Check to make sure that you are not sending data to the browser before you call these functions.

jatar_k

12:46 am on Nov 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



remeber that even an empty line or a space before your <? is content sent to the browser.

ahmedtheking

9:56 am on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh rite, ill try that! thanks :D