我重复使用了一年前创建的旧CodeIgniter项目的登录系统,我有点生疏,但我查找了一段时间,我找不出原因用户名和密码不起作用。我使用的注册码是使用MD5加密的,我已经设置了一个加密密钥。Codeigniter登录不起作用
登录视图代码:
<?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;
}
}
它返回我的 “无效的登录详细信息” 的消息,并没有错误。
打印上次查询并运行在phpMyAdmin该查询检查的一部分。 –