ajax - user_session 不工作(Codeigniter,Ajax)
问题描述
新手在这里。我有一个ajax登录功能。登录没有问题,但会话存在。我遇到的问题是每当我调用会话时,它都不起作用。我用 print_r($this->session->all_userdata()); 查看 user_session 数据,但我没有得到任何信息。有什么我错过的吗?任何帮助将不胜感激。太感谢了。
意见:
阿贾克斯:
<script>
$(function(){
$("#doLogin").submit(function(e){
e.preventDefault();
$("#btnSubmit").css('display', 'none');
$("#submit_preloader").css('display','block');
$("#errorMessage").html('');
$.ajax({
url: "<?=site_url('account/dologin')?>",
data: $(this).serialize(),
dataType: "json",
type: "post",
async: false,
success: function(data){
if(data.response == "true") {
$("#doLogin")[0].reset();
$("#submit_preloader").css('display','none');
$("#errorMessage").css('display', 'block');
$("#errorMessage").html(data.errors);
setTimeout("window.location.href='<?=site_url('profile/prof')?>'" ,300);
} else {
$("#errorMessage").css('display', 'block');
$("#errorMessage").html(data.errors);
//alert("SORRY. " + data.errors);
$("#btnSubmit").css('display','block');
$("#submit_preloader").css('display','none');
}
},
});
});
});
</script>
控制器:
public function dologin()
{
//die('if tinawag to');
$this->output->enable_profiler(false);
$this->form_validation->set_rules('formUsername','Username','required|min_length[8]|max_length[35]');
$this->form_validation->set_rules('formPassword','Password','required|min_length[8]|max_length[35]');
$success = $this->form_validation->run($this);
if($success)
{
$pdata = array(
'username' => trim($this->input->post('formUsername')),
'password' => MD5(trim($this->input->post('formPassword'))),
);
// $data['response'] = "false";
// $data['errors'] = 'Processing';
if($this->accounts->checkUsername(trim($this->input->post('formUsername')))) {
$user = $this->accounts->checkUserData($pdata);
if($user){
$this->accounts->log_session();
//log user
$data['response'] = "true";
$data['errors'] = 'login successful';
} else {
$data['response'] = "false";
$data['errors'] = "Invalid User Password";
}
} else {
$data['response'] = "false";
$data['errors'] = "Username does not exist.";
}
} else {
$data['response'] = "false";
$data['errors'] = validation_errors();
}
echo json_encode($data);
}
Model: // User_session 数据
function checkUserData($data)
{
$sql = "select * from users where username like ? and password like ?";
$Q = $this->db->query($sql, array($data['username'], $data['password']));
if($Q->num_rows() > 0) {
$R = $Q->row_array();
$arrSession = array(
'uid' => $R["userID"],
'username' => $R["username"],
'mobile' => $R["mobile"],
'password'=> $R["password"],
'currentPoints' => $R['currentPoints'],
'mobile' => $R['mobile'],
'firstname' => $R['firstname'],
'lastname' => $R["lastname"],
'email' => $R["email"],
'account_type' => $R["account_type"],
'commCurrentBalance' => ["commCurrentBalance"]
);
$this->session->set_userdata($arrSession);
//print_r( $this->session->all_userdata());
return $R;
} else {
return false;
}
}
解决方案
您需要在控制器的 __construct 函数中包含会话库。
推荐阅读
- kubernetes - 重启集群时出错:等待 apiserver:等待条件超时
- javascript - 当我尝试将它放在 tr td 上时,为什么 jQuery 中继器的添加按钮不起作用?
- arraylist - 如何使用 Leitner System 将数组拆分为子数组并显示项目?
- laravel-5 - 如何从路由中调用文件
- c# - asp.net mvc 5,验证非必填字段(可选字段)
- excel - 创建 en excel 公式并将此公式传递给模块 f2() 的函数出错
- angular - 以 Angular 反应形式一次显示一个问题
- php - Laravel 总是从 JSON 帖子中获取空
- c# - 如何在 Quickbooks 桌面发票中添加 SO Number 字段。我无法在发票添加的 api 参考中找到它
- exception - MathLinkException C# - Mathematica