簡體   English   中英

登錄codeigniter,如何比較密碼

[英]login in codeigniter, how compare passwords

我嘗試使用本教程http://www.iluv2code.com/login-with-codeigniter-php.html編寫登錄名,但是驗證密碼有問題。 我的數據庫看起來完全一樣,當我一直嘗試登錄時,我都擁有'Invalid username or password'但是如果我執行if(1)而不是VerifyLogin.php if($result)VerifyLogin.php可以了,所以我認為問題出在驗證方面。 有人知道如何改善嗎? 或如何僅從該數據庫獲取密碼? 我嘗試比較手動...

模型:

function __construct() {
   parent::__construct();
   $this->load->database();
}

function login($username, $password)
{
   $this -> db -> select('id, username, password');
   $this -> db -> from('users');
   $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();  //row

   }
   else
   {
     return false;
   }
}
}
?>
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $result = $this->users->checkloginauth($username, $password);        

        if(isset($result) && count($result) > 0)
        {
            $this->session->set_userdata('admin_id', $result[0]['id']);
            $this->session->set_userdata('firstname', $result[0]['firstname']);
            $this->session->set_userdata('lastname', $result[0]['lastname']);
            $this->session->set_userdata('email', $result[0]['email']);
            $this->session->set_userdata('status', '1');

            redirect(site_url('home'), 'refresh');

            exit;
         }
         else
         {
            $data['errormsg'] = "Invalid UserName or password.";
            $this->load->view('login',$data);
         }

如果您使用md5,則寫$ password = md5($ this-> input-> post('password'));

在用戶模型中

public function checkloginauth($username, $password)
{

    $this->db->select();
    $this->db->from($this->users." AS u");

    $this->db->where('u.username', $username);
    $this->db->where('u.password', $password);
    $result = $this->db->get();

    return $result->result_array();
}

result_array()返回數組中的值,因此數據存儲在$ result [0]中

使用isset

if(isset($query))
{
   return $query->result();  //row
}

這不能允許Usename和密碼不應該像字符和數字一樣..如果相同只能得到結果

public function checkloginauth($username, $password)
{
    $this->db->select();
    $this->db->from($this->users." AS u");

    $this->db->where('u.username LIKE BINARY', $username);
    $this->db->where('u.password LIKE BINARY', $password);
    $result = $this->db->get();

    return $result->result_array();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM