0

如果用户不存在,它应该在登录屏幕上返回一个错误,但用户随后会被带到主页,就好像他们以前在我的标志控制器中注册过一样。登录控制器 - 验证凭据功能 - 和我的模型 - 功能 can_log_in() - 将说明它应该反弹的错误

模型:

  class Membership extends CI_Model
{
function Membership()
{
    parent::__construct();
}


public function can_log_in()
{
    $this->db->select('*')->from('membership');
    $this->db->where('username', $this->input->post ('username'));
    $this->db->where('password', md5($this->input->post ('password')));

    $query = $this->db->get('membership');
    if ($query->num_rows() == 1)
    {
        return true;
    }

    else{

        return false;

    }

}

登录控制器:

 class Login extends CI_Controller 
 {

   function Login()
    {
     parent::__construct();

      $this->load->model('membership');
    }


    function loguserin()

    {

$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');


$username = $this->input->post('username');
$password = $this->input->post('password');
      if ($this->form_validation->run()==TRUE)
     {
          $this->load->model('membership');
           if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password'))
       { // this is unepected despite a new if statement called
        $this->session->set_userdata('status', 'OK');
        $this->session->set_userdata('username', $username);  
        redirect('home');
        } else //this else is unexpected
          {
       $this->session->set_userdata('status', 'NOT_OK');

      $this->session->set_flashdata('Incorrect username/password');
       $this->index();
      }
    } else {
   $this->index; 

   }

  }







        function logout()
      {
$this->session->sess_destroy();
redirect ('start');
       }


      function index()
      {
      $this->load->view('shared/header');
       $this->load->view('account/logintitle');
      $this->load->view('account/loginview');
        $this->load->view('shared/footer');
       }
      }

再次感谢 :) 只是用户如果不在表格中应该无法登录主页

4

2 回答 2

1

您的逻辑无处不在,并且您误解了表单验证的概念。表单验证验证表单输入,它不验证凭据。老实说,您拥有的 validate_credentials 函数有点毫无意义,因为您无法从 run 函数外部调用表单验证错误。所以你不妨干脆让它完全消失。

首先,form_validation 之后的初始调用应该是这样的:

if ($this->form_validation->run()==TRUE)
{
    $this->load->model('membership');
    if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password')))
    {
    $this->session->set_userdata('status', 'OK');
    $this->session->set_userdata('username', $username);  
    redirect('home');
    } else {
    $this->session->set_userdata('status', 'NOT_OK');// you'll need to do something
   // here to tell them their credentials are wrong as I said this won't work through
   // the form validation, perhaps look into using $this->session->set_flashdata()
    $this->index();
    }
} else {
$this->index; //reload the index to display the validation errors

好的,负责将数据发送到登录函数,现在该函数实际上需要接收数据,并且由于它不是发送数据的第一页,因此帖子字符串将为空,这就是我在函数中传递数据的原因以上。模型中唯一需要更改的部分如下:

public function can_log_in($username,$password)
{
$this->db->select('*')->from('membership');
$this->db->where('username', $username);
$this->db->where('password', md5($password));
于 2012-11-22T21:52:17.240 回答
0

尝试更换

if ($this->form_validation->run())

if ($this->form_validation->run() && validate_credentials())
于 2012-11-22T18:25:01.923 回答