我重新使用我一年前制作的旧CodeIgniter项目的登录系统,我有点生疏,但我已经找了一段时间,我无法弄清楚为什么用户名和密码不会工作 . 我使用的寄存器代码使用MD5加密,我设置了加密密钥 .
登录视图代码:
<?php echo form_open('Login_controller/Login'); ?>
<label for="Username">Username:</label>
<input type="text" id="Username" name="Username" autofocus placeholder="Your username"/>
<label for="Password">Password:</label>
<input type="password" id="Password" name="Password" placeholder="Your password" />
<input type="submit" name="Login" value="Login" >
<?php echo validation_errors(); ?>
<p>Not an existing user? <a href="<?php echo site_url("Register_controller")?>">REGISTER</a><p>
</form>
登录控制器代码:
function Login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Username', 'Username', 'trim|required');
$this->form_validation->set_rules('Password', 'Password', 'trim|required|callback_CheckDatabase');
if($this->form_validation->run() == FALSE)
{
$this->load->view('Login_view'); //Reloads the login view with validation errors if the login attempt is unsuccessful.//
}
else
{
redirect('Home_controller'); //Redirect to the homepage on successful login attempt.//
}
}
function CheckDatabase($password) //This function is only run when password validation is correct.//
{
$username = $this->input->post('Username'); //Sets the username as a $username.//
$result = $this->User_model->Login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'id' => $row->UserID
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}
else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
{
$this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
return false;
}
}
用户型号代码:
function Login($username, $password)
{
$this -> db -> select('UserID, Username, Password');
$this -> db -> from('CIUsers');
$this -> db -> where('Username', $username);
$this -> db -> where('Password', md5($password));
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
它返回我的“无效登录详细信息”消息,没有错误 .
2 回答
我用过print
并意识到我将数据库中的密码长度限制为20而没有意识到加密使其长于20,将其更改为50修复了问题 .
首先,md5对于登录是不安全的,你应该尝试使用密码哈希技术 .
试试这样
并在你的模型中这样
如需完整教程,请阅读http://w3code.in/2015/09/create-login-and-registration-with-codeigniter/