Click to See Complete Forum and Search --> : Redirecting from a login page


tobyw_1969
12-30-2003, 09:58 AM
I am trying to create a member log-in page, which re-directs the user depending on what they enter.

But when I do this



if($password == $expected_password){
header("Location: http://www.netcartoon.net/");
exit;
}else{
echo "user not found";
}


I keep getting an error

Warning: Cannot add header information -
headers already sent by (output started at
/home/httpd/vhosts/netcartoon.net/httpdocs/naaas/new/login.php:16)
in /home/httpd/vhosts/netcartoon.net/httpdocs/naaas/new/login.php on line 26

From the PHP documentation, I thought the exit; command was intended to stop the rest of the script causing problems, or it may be something totally different. Can anyone see what I am doing wrong?

Thanks

Toby

pyro
12-30-2003, 10:15 AM
Headers must be set before any content is sent to the browser.

tobyw_1969
12-30-2003, 11:21 AM
Thanks pyro - does that mean I can't do it this way?

I want to send the user to a different page depending on the result of their log-in. The reason for this is that on my page 'login.php' the form submit action is to laod 'login.php' again, which then checks if they have entered a username and password. If they haven't, it displays a message, otherwise it needs to direct them to the successful log in page. Is there a better way?

Any ideas on how to do this the best way?

Toby

pyro
12-30-2003, 11:27 AM
Just put the PHP code at the very top of the page, and you should be good to go.

tobyw_1969
12-30-2003, 12:28 PM
Excellent pyro!!Thanks it works fine - I had an echo command in there.

One problem though - how can I get it to use a variable URL? I tried this

header("Location: ".$returnurl);

and

$returnstring= "Location: ".$returnurl;
header($returnstring);




but neither works. Do you know what I need to do here?

pyro
12-30-2003, 12:41 PM
Your first one should have worked. Are you sure $returnurl is set?

tobyw_1969
12-30-2003, 02:27 PM
You are a star pyro! Yes, I had an extra t in http://

Thanks for all your help. Got it all working. One whole day just for a login page....is that a record? :P

PS. Is there a better way to validate that the user has entered the username and password? I'm loading the same page again from the submit button....and changing the message if either variable is missing - but it makes it complex once they enter the details cos I have to do this re-direct...is there a way to validate directly in the actual form??

pyro
12-30-2003, 02:29 PM
Usually what I do (if possible) is have the form's action point to the current page, and add my form's handler to the top of the page. This way, I can echo error messages out, and also re-fill the values of the form for the entries that they got correct.

tobyw_1969
12-30-2003, 05:37 PM
That's good - I'm doing it same way, so it makes me feel better to know people who know what they are doing use the same method. But it seems to get pretty complex based on all the possibilities. Anyway, thanks for all the help Pyro - got it all working now :)

pyro
12-30-2003, 06:06 PM
You bet, I was happy to help. :)