starting session with AJAX

starting session with AJAX

am 29.08.2009 19:58:49 von John Pillion

------=_NextPart_000_0019_01CA2897.B0874BF0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Ok, so I've got an authentication/login form that is "powered by" ajax. The
user logs in, is authenticated, and the last step is to start a session and
save the necessary information in the $_SESSION vars.

For some reason, it appears (and almost of makes sense) that the session
that is started via the AJAX is lost once the ajax is complete.

AJAX calls the php, passing username and password
Php executes all the necessary authentication, etc etc
If, login is valid, it calls mySessionStart() (see below)
Checks to make sure the session has been started using isLoggedIn() (below),
which returns true
AJAX closes, receiving a "successfully logged in" message.
AJAX turns around and makes a second call, calling isLoggedIn() again, and
the session is gone

What I'm *guessing* is that because the PHP is not running on the active
page, but "in the background" the session is not being set for the active
page. Is there a way to pass the session back to the browser?

- John


Debugging code has been removed for readability:
**********************************

function mySessionStart($persist, $sessionID, $sessionKey, $debug=0){

session_start();

$_SESSION['sessDBID'] = $ sessionID;
$_SESSION['sessKey'] = $ sessionKey;
$_SESSION['persist'] = $persist;

// if persist, set cookie
if ($persist){
return myCreateCookie($persist, $ sessionID, $ sessionKey,
$debug);
}else{
return true;
}
}


********************************



function isLoggedIn($debug = 0){

global $COOKIE_NAME;

// if there is an active session.
if (isset($_SESSION) && $_SESSION['sessDBID'] != '' &&
$_SESSION['sessKey'] != ""){

//. check the contents
return authenticate($_SESSION['sessDBID'],
$_SESSION['sessKey']);

// or, check for (persistent) cookie.
}elseif (isset($_COOKIE[$COOKIE_NAME]) && $_COOKIE[$COOKIE_NAME] !=
""){

$sessInfo = split('-', $_COOKIE[$COOKIE_NAME]);

// . and check the contents
if(authenticate($sessInfo[1], $sessInfo[0], $debug)){

// reset the cookie
mySessionStart(true, $sessInfo[1], $sessInfo[0],
$debug);
}else{
// cookie authentication failed
return false;
}

}else{
// there is no session or cookie
return false;
}
}

------=_NextPart_000_0019_01CA2897.B0874BF0--

RE: starting session with AJAX

am 29.08.2009 21:22:47 von John Pillion

------=_NextPart_000_0026_01CA28A3.6B028CF0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

I found two small errors in the isLoggedIn(), which are corrected below.
They don't have any effect on the issue at hand though.


**************************
function isLoggedIn($debug = 0){

global $COOKIE_NAME;

// if there is an active session.
if (isset($_SESSION) && $_SESSION['sessDBID'] != '' && $_SESSION['sessKey']
!= ""){

//. check the contents
return authenticate($_SESSION['sessDBID'], $_SESSION['sessKey']);

// or, check for (persistent) cookie.
}elseif (isset($_COOKIE[$COOKIE_NAME]) && $_COOKIE[$COOKIE_NAME] != ""){

$sessInfo = split('-', $_COOKIE[$COOKIE_NAME]);

// . and check the contents
if(authenticate($sessInfo[0], $sessInfo[1], $debug)){

// reset the cookie
return (mySessionStart(true, $sessInfo[0], $sessInfo[1], $debug));
}else{
// cookie authentication failed
return false;
}

}else{
// there is no session or cookie
return false;
}
}


------=_NextPart_000_0026_01CA28A3.6B028CF0--

RE: starting session with AJAX

am 29.08.2009 21:34:37 von John Pillion

------=_NextPart_000_002B_01CA28A5.122CC800
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Nevermind. It was a simple mistake - I had "session_start()" on the page
the ajax was calling from, but not at the beginning of the php script it was
calling to.

------=_NextPart_000_002B_01CA28A5.122CC800--