我正在努力使用此代码超过12个小时,任何帮助都将深表感谢。在我发布代码之前几句话。我的后台身份验证代码非常相似,现在我正在进行站点成员身份验证。后台工作正常,前端没有。
这是CodeIgniter应用程序,是我做过很多次的事情。如果后端代码不起作用,我怀疑服务器有不同的设置,但后端代码工作。我不是ci的初学者,我想我在某个地方做了一些愚蠢的错误......
这是登录功能:
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('memberusername', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('memberpassword', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
// if validation is passed
if ($this->form_validation->run() != FALSE)
{
$this->db->where('memberUserName', $this->input->post('memberusername'));
$this->db->where('memberOldPassword', strtoupper(md5("EBOSS/".$this->input->post('memberpassword')."/EBOSS")));
$query = $this->membersModel->get();
if($query)
{
$data = array(
'memberID' => $query[0]['memberID'],
'memberUserName' => $query[0]['memberUserName'],
'memberOldPassword' => $query[0]['memberOldPassword'],
'memberEmail' => $query[0]['memberEmail'],
'isUserLoggedIn' => TRUE
);
$currentUser = array();
//echo "Here after the data?";
//die();
$this->session->set_userdata('currentMember', $data);
$currentMember = $this->session->userdata('currentMember');
// print_r($currentMember);
//die();
echo json_encode(array("success" => true));
} else {
echo json_encode(array("success" => false, "error" => "Wrong credentials"));
}
// form validation has failed
} else {
$errorMessage = "Wrong Username or passwrod!";
}
} // end of function loginCheck
这是javascript代码:
// user fill both fields, user name and memberpassword, so form can be submitted
jQuery("#formLogin").submit(function(e){
e.preventDefault();
var memberusername = jQuery(this).find("#memberusername").val();
var memberpassword = jQuery(this).find("#memberpassword").val();
var obj = {memberusername: memberusername, memberpassword: memberpassword};
var url = jQuery(this).attr("action");
jQuery.post(url, obj, function(r){
if(r.success) window.location.replace('http://www.example.net/memberAccount/memberDashboard');
else jQuery('#errorMessageTop').fadeIn();
}, 'json')
})
我收到错误Uncaught TypeError:无法读取null
的属性“success”最后,这是用户帐户页面上的输出:
Array
(
[session_id] => 711a8349fe7414802928ac27b7bd2c4f
[ip_address] => 62.103.42.2
[user_agent] => Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0
[last_activity] => 1416509476
[currentUser] => Array
(
[authbackofficeuserID] => 428
[authbackofficeuserUserName] => mysuername
[authbackofficeuserPassword] => 69e3184e2cdc3605693ba24887c519aaafc89477
[authbackofficeuserEmail] => me@mail.com
[authbackofficeuserFirstName] => My
[authbackofficeuserLastName] => Name
[isUserLoggedIn] => 1
)
)
当前用户是在后台办公室登录的用户数据,但无法找到应该在这里的currentMember。
事情更奇怪,如果我注释掉javascript,数据会显示在数组输出中。一旦我删除评论,并且我退出,当我再次尝试登录时,该数据(currentMember)就消失了。
任何帮助都将深受赞赏。
问候,约翰
答案 0 :(得分:0)
我认为您错过了 $ query-&gt; result_array();
$members = $query->result_array();
if(count($members) != 0){
//set your session logic here
}
记得使用MVC;我看到你正在混合 $ this-&gt; db 函数(Model)和 $ this-&gt; form_validation 函数(Controller)
if ($this->form_validation->run() != FALSE) {
$this->db->where('memberUserName', $this->input->post('memberusername'));
$this->db->where('memberOldPassword', strtoupper(md5("EBOSS/".$this->input->post('memberpassword')."/EBOSS")));
$query = $this->membersModel->get();
$members = $query->result_array();
if(count($members) != 0){
$data = array(
'memberID' => $members[0]['memberID'],
'memberUserName' => $members[0]['memberUserName'],
'memberOldPassword' => $members[0]['memberOldPassword'],
'memberEmail' => $members[0]['memberEmail'],
'isUserLoggedIn' => TRUE
);
$this->session->set_userdata('currentMember', $data);
echo json_encode(array("success" => true));
}else {
echo json_encode(array("success" => false, "error" => "Wrong credentials"));
}
} else {
echo json_encode(array("success" => false, "error" => "Incorrect inputs"));
}
并检查 memberDashboard 控制器以获取会话