我正在尝试使用 CodeIgniter 和 jQuery 进行用户身份验证。我设法做到了以下几点:如果用户输入了错误的用户名或密码,他会留在登录页面上,并且使用 jquery,他会收到通知说他输入了错误的用户名或密码。
我的问题是成功的,也就是说,如果他输入正确的用户名和密码,我设法通过简单的 javascript 重定向在登录页面上发送用户,但这不是我想要的。如果我使用带有 javascript 的简单重定向,我无法填写用户会话数据,我需要在所有其他页面上登录后跟踪用户。
我想如果可能的话,如果用户输入正确的用户名和密码,jquery 什么都不做,并且只有在用户输入错误的用户名或密码时才激活功能,如果可能的话。
这是我拥有的代码。JS代码先:
formLogin.on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'loginCheck',
data : formLogin.serialize(),
dataType : 'json',
success : function(r) {
if (r.status) {
// this works, but I would like this part to do nothing
// just to leave my PHP controller to redirect the user
window.location = 'http://www.linkedin.com';
} else {
$('#errorMessageTop').fadeIn();
}
}
});
});
控制器代码是:
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('username', $this->input->post('username'));
$ids[1] = $this->db->where('password', md5($this->input->post('password')));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'username' => $this->input->post('username'),
'isUserLoggedIn' => true
);
$isAuthenticated = true;
if ($isAuthenticated) {
$return['status'] = true;
$return['message'] = 'You have successfully been logged in!';
exit(json_encode($return));
$this->session->set_userdata($data);
$data['title'] = "Welcome to dashboard!";
$data['main_content'] = 'dashboard';
$this->load->vars($data);
$this->load->view('backOffice/template');
}
} else {
$return = array(
'status' => false,
'message' => 'Wrong Username or Password'
);
exit(json_encode($return));
}
} else {
$errorMessage = "Second Message Wrong username or pwd...!";
$errorMessage = json_encode($errorMessage);
}
} // end of function loginCheck
任何帮助将不胜感激。
问候,佐兰
================
来自控制器的新代码:
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('username', $this->input->post('username'));
$ids[1] = $this->db->where('password', md5($this->input->post('password')));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'username' => $this->input->post('username'),
'isUserLoggedIn' => true
);
$this->session->set_userdata($data);
echo json_encode(array("success" => true));
$data['title'] = "Welcome to dashboard!";
$data['main_content'] = 'dashboard';
$this->load->vars($data);
$this->load->view('backOffice/template');
} else {
echo json_encode(array("success" => false, "error" => "Wrong credentials"));
}
} else {
$errorMessage = "Second Message Wrong username or pwd...!";
$errorMessage = json_encode($errorMessage);
}
} // end of function loginCheck
和js代码:
$("#formLogin").submit(function(e){
e.preventDefault();
var username = $(this).find("#username").val();
var password = $(this).find("#password").val();
var obj = {username: username, password: password};
var url = $(this).attr("action");
$.post(url, obj, function(r){
if(r.success) window.location.replace('http://www.linkedin.com');
else $('#errorMessageTop').fadeIn();
}, 'json');
})