我在管理网站登录时遇到了一些麻烦。我尝试了几种不同的方法和查询,这些方法和查询似乎都导致了同样的问题:我用来检查username
和password
的方法总是返回false
。
为了更容易理解,这是我正在尝试做的事情:
username
和password
callback
函数为表单callback
函数调用模型方法TRUE
,否则返回FALSE
。显然它总是返回FALSE
,因为登录永远不会有效......
我正在使用CodeIgniter。
控制器文件:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
}
public function index()
{
$this->form_validation->set_rules('username', 'Username', 'callback_login_check[password]');
if ($this->form_validation->run() == FALSE) {
//loading view as long as it is not correct
$this->load->view('login');
} else {
//temporary successful login page
$this->load->view('success');
}
}
public function login_check($usr, $pwd)
{
$this->load->database();
$this->load->model('EZ_Query');
$result = $this->EZ_Query->get_user_details($usr, $pwd);
if ($result == TRUE) {
return TRUE;
} else {
$this->form_validation->set_message('login_check', 'Password does not match Username');
return FALSE;
}
}
}
模型文件:
class EZ_Query extends CI_Model {
public function get_user_details($usr, $pwd)
{
$sql = "SELECT *
FROM PROFIL
WHERE USER = '$usr'
AND MDP = '$pwd'";
$query = $this->db->query($sql);
if ($query->num_rows() == 1) {
$row = $query->row();
//session variables
$data = array('name' => $row->NOMPROFIL,
'fname' => $row->PRENOMPROFIL,
'type' => $row->TYPEPROFIL);
$this->session->set_userdata($data);
return TRUE;
} else {
return FALSE;
}
}
}
不太有用,这是登录视图页面的一部分:
<body>
<?php
echo validation_errors();
echo form_open('Login');
echo form_label('Username', 'username');
echo form_input('username')."<br />";
echo form_label('Password', 'password');
echo form_password('password')."<br />";
echo form_submit('sumbit', 'Enter');
echo form_close();
?>
</body>
抱歉英语不好,谢谢你的帮助。
答案 0 :(得分:2)
我在这里改变了一些事情让你尝试
我希望您使用好的哈希密码 NOT MD5 使用http://php.net/manual/en/function.password-hash.php和http://php.net/manual/en/function.password-verify.php
文件名:Login.php
<?php
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
// Autoload the session because you may need it else where
}
public function index() {
// remove the word password from callback as it on the username
// login all ways good to use required
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_login_check');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE) {
//loading view as long as it is not correct
$this->load->view('login');
} else {
//temporary successful login page
$this->load->view('success');
}
}
public function login_check()
{
$usr = $this->input->post('username');
$pwd = $this->input->post('password');
$this->load->database(); // Autoload database best.
// Filename of model should be Ez_query.php and same with class only first letter upper case
$this->load->model('ez_query');
$result = $this->ez_query->get_user_details($usr, $pwd);
if ($result == TRUE) {
return TRUE;
} else {
$this->form_validation->set_message('login_check', 'Password does not match Username');
return FALSE;
}
}
}