CodeIgniter登录系统

时间:2017-05-23 06:26:59

标签: codeigniter

我是CodeIgniter的新手,我试图在我的网站上创建一个登录系统。 我正在尝试在codeigniter中创建一个登录页面

这是我的代码..

控制器

public function login(){
$this->load->database();
$this->load->helper('url');
$this->load->model('Login');
 $data = $this->Login->login();
 if(! $data){
        // If user did not validate, then show them login page again
        $this->load->view('login');
    }else{
        // If user did validate, 
        // Send them to members area
       redirect('Home/index');
    }    

 }

查看

  <form id="contact-form" method="post" action="<?php echo 
  site_url('Home/login');?>">
                        <div class="row clearfix">
                            <div class="form-group col-md-6 col-sm-12 col-
  xs-12">
                                <div class="field-label">User Name *</div>
                                <input type="text" name="username" 
  placeholder="User Name" required>
                            </div>
                            <div class="form-group col-md-6 col-sm-12 col-
  xs-12">
                                <div class="field-label">Password *</div>
                                <input type="password" name="pass" 
  placeholder="Password" required>
                             </div>


                            <div class="form-group col-md-12 col-sm-12 col-
  xs-12 text-right">
                                <button class="normal-btn theme-btn" 
  type="submit" name="submit-form">Login</button>
                            </div>
                        </div>

                    </form>

模型

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

 public function login(){
    // grab user input
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('pass'));

    // Prep the query
    $this->db->where('doc_email', $username);
    $this->db->where('pass', $password);

    // Run the query
    $query = $this->db->get('doctors');
    // Let's check if there are any results
    if($query->num_rows == 1)
    {
        // If there is a user, then create session data
        $row = $query->row();
        $data = array(
                'doc_id' => $row->doc_id,                    
                'doc_email' => $row->doc_email
                );
        $this->session->set_userdata($data);
        return true;
    }
    // If the previous process did not validate
    // then return false.
    return false;
}
}
当我输入正确的用户名和密码

时,没有任何反应

2 个答案:

答案 0 :(得分:1)

试试这个

<?php 
 class Login extends CI_Model{
 function __construct(){
    parent::__construct();
 }

 public function login(){
    // grab user input
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('pass'));

    // Prep the query
    $this->db->where('doc_email', $username);
    $this->db->where('pass', $password);

    // Run the query
    $data = $this->db->get('doctors')->row();
    //my changes is here..
    if (count($data) > 0) {
        $sdata = array(
                'doc_id' => $data->doc_id,                    
                'doc_email' => $data->doc_email
                );
        $this->session->set_userdata($sdata);
        return true;
    } else {
        return false;
    }
  }
}

答案 1 :(得分:0)

在您的模型中,更改此内容:

if($query->num_rows == 1)

进入这个:

if($query->num_rows() == 1)

你可以互换使用属性和方法..