Jump to content

Archived

This topic is now archived and is closed to further replies.

hey_suburbia

session_start(); before or after redirect?

Recommended Posts

I have a very basic login that determines if the email address exists in the DB and if it does they get redirected, if not they get redirected elsewhere:

[code]$fetch_exist=mysql_query("SELECT email FROM memberlist WHERE email = '$email'");

//then check if the email is there
if(mysql_num_rows($fetch_exist)>0)
{

    //it's in the database, bring them to logged in area
header('Location: http://site.com/directory/page.php');
exit;
}

else
{ //it's not in the database, bring them to sign-up
header('Location: http://site.com/page2.html');
exit;
}[/code]

This works great.

The problem occurs when I try to start the session, which is this:

[code]session_start();
$_SESSION ['email'] = $email;[/code]

I tried putting it before the if/else statement, I tried putting it into the if statement.  All return blank fields in the database.

On my sign up page, I have the same thing (without the redirect and it works great).

So, I guess my question becomes, where do I start the session on a page that redirects.  I tried to start it on the page that you get redirected to, but I would have to pass the $email variable to that page...

Any thoughts?

THANKS!!!

Share this post


Link to post
Share on other sites
You have to put session_start(); before anything is sent to the browser. I've never had a problem setting the session and then redirecting whatsoever.

Share this post


Link to post
Share on other sites
thanks.

So if I put the session_start() before this:

[code]$fetch_exist=mysql_query("SELECT email FROM memberlist WHERE email = '$email'");

//then check if the email is there
if(mysql_num_rows($fetch_exist)>0)
{

    //it's in the database, bring them to logged in area
header('Location: http://site.com/directory/page.php');
exit;
}

else
{ //it's not in the database, bring them to sign-up
header('Location: http://site.com/page2.html');
exit;
}[/code]

It should work, right?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.