Jump to content


Photo

session_start(); before or after redirect?


  • Please log in to reply
3 replies to this topic

#1 hey_suburbia

hey_suburbia
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 10 August 2006 - 04:00 PM

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:

$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;
	}

This works great.

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

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

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!!!

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 10 August 2006 - 04:11 PM

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.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 hey_suburbia

hey_suburbia
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 10 August 2006 - 05:14 PM

thanks.

So if I put the session_start() before this:

$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;
	}

It should work, right?

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 10 August 2006 - 05:16 PM

Yep :)

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users