Jump to content

Ajax Cookies And Sessions Die After I Refresh O.o *fixed*


MrFish
 Share

Recommended Posts

Well I know that's not exactly a correct statement. But I'm using ajax to make a session for a login script. And it works but when I refresh everything just forgets. It forgets that I set a session. And I know the session is set because when you login successfully the echo response looks like this-

if($affected != 1){	echo '<form name="login">	<input type="text" name="username" value="username"><br />	<input type="password" name="password" value="password"><br /><br />	<input type="button" name="login" value="Login" onClick="ajaxlogin()"><br />	</form><font color="red">The username or password was incorrect!</font>';} else {	session_start();	$_SESSION['username'] = $username;	echo "You're logged in, <b>" . $_SESSION['username'] . "</b>.";}

And I see my username and everything. But when I refresh the page this script doesn't see that a session exists.

if(isset($_SESSION['username'])){echo 'You are already logged in, <b>.' . $_SESSION['username'] . '</b>.';} else {echo '<form name="login">';echo '<input type="text" name="username" value="password"><br />';echo '<input type="password" name="password" value="password"><br /><br />';echo '<input type="button" name="login" value="Login" onClick="ajaxlogin()">';echo '</form>';}

I'm 99% sure that's correct syntax. What's wrong with it? Why won't sessions OR cookies exist after I refresh the page? I tried with cookies before but I've been told you need to use sessions for login so I am.

Edited by MrFish
Link to comment
Share on other sites

When are you starting the session? Don't start it when they log in, start it as soon as they get to the page. Don't wait for the ajax request to start the session, start it as soon as they show up.

Link to comment
Share on other sites

I tried to put session_start(); before the page begins but it didn't work.edit:Fixed. I forgot I needed to write session_start() before I checked if my session existed.

Edited by MrFish
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...