I am using session_start
to create a session for the user when it is authenticated on the server, however the session is not being maintained in the page exchange, I am using AJAX requests to authenticate, then:
login.js + ajax - > login.php [create session if logged in] [message error if not logged in] login.js - > [if logged in redirects for panel.php]
The session is created in login.php (I tested with empty($_SESSION)
), but in page panel.php the session no longer exists, I used print_r($_SESSION)
and the variable is not recognized.
Does anyone know the cause of the error?
login.js:
$(document).ready(function(){
$("#go").click(function(e){
e.preventDefault();
client = new clientSnotes("log");
client.sendLogin($("#email").val(), $("#password").val(), "log");
});
});
functions used by login.js:
function clientSnotes(){
this.parseUser = function(data, log){
var xml = $.parseXML(data);
xml = $(xml).contents();
var error = xml.attr('error');
//No error
if(error === '0'){
var user = new userFields(xml.attr('id'), xml.attr('name'), xml.attr('email'), xml.attr('password'));
window.location.href = "panel.php?" + user.getDataUrl();
}
else{
$("#" + log).css("display", "inline");
$("#" + log).html(xml.contents());
}
};
this.assemblyRequisition = function(parser, url, data, error_log){
$.ajax({
type: 'post',
url: url,
data: data,
success: function(data){
parser(data, error_log);
},
error: function(){
alert("Failed to send data to server.");
}
});
};
this.sendLogin = function(email, password, error_log){
this.assemblyRequisition(this.parseUser, 'scripts_php/login.php', 'email=' + email + '&password=' + password, error_log);
};
login.php
if(UserDataHandler::selectByEmail($_POST['email']) && UserDataHandler::selectByPassword($_POST['password'])){
$user = UserDataHandler::selectByEmail($_POST['email'])[0];
$assembler->addAttribute("error", "0");
$assembler->addAttribute("id", $user->id);
$assembler->addAttribute("name", $user->name);
$assembler->addAttribute("email", $user->email);
$assembler->addAttribute("password", $user->password);
session_start();
//testando
if(empty($_SESSION))
$assembler->addAttribute ("session", "no");
else
$assembler->addAttribute ("session", "ok");
$_SESSION['con'] = 1;
}
...
echo $assembler->assembly("result");//escreve o resultado da operação na página
panel.php
<div id="content">
<?php
print_r($_SESSION);
?>
</div>