i have created login page which sends ajax request to a php page for login verification. On that php page i m creating session, and sending response as per login verification. if user is authenticated i m redirecting it to home page from java script from where i send ajax. but on that homepage i cant get that session object... why? can u tell me solution to retrieve that session on home page
+1
A:
I am not sure I do it the right way, but it works for me this way (I hope I didn't forget anything):
Here's what my login.php does :
header('Content-type: text/json');
// here : import files I need
session_start();
// here : load some parameters into session variables
// here : init mysql connection
// here : get user and password from $_POST and check them against the database
// If the user can't connect, return an error to the client
if ( ! $ok )
{
echo '{ "ok": "N" }';
exit;
}
$_SESSION['user'] = $user;
echo '{ "ok": "O" }';
?>
Then when I access another php file, here's how it begins :
header('Content-type: text/json');
// again, required files go here
session_start();
if ( ! isset($_SESSION['user'] )) {
echo '{ "ok": "N" }';
exit;
}
$user=$_SESSION['user'];
....
Every ajax call I make checks if the result tells me the user isn't connected, and returns to the login page if he isn't.
$.ajax({
type: "POST",
url: "myphp.php",
dataType: "json",
data: somedatatopost,
success: function(pRep){
if (!pRep) {
alert("No response from the server.");
return;
}
if (pRep.ok=="N") {
window.location = '/index.html';
return;
}
// here is where I handle a successful response
}
});
For the login ajax call, I have :
[....]
// here is where I handle a successful response
window.location='mynewpage.html'
David V.
2010-03-16 10:29:55