2013-06-27 90 views
2

我是CodeIgniter和使用CodeIgniter_2.1.3。我正在關注NETTUTS CodeIgniter教程。我想開發一個codeIgniter的登錄系統,它不需要md5()函數[沒有加密密碼]。然後我使用md5()函數創建帶有加密密碼的新表。然後登錄代碼不起作用。我的代碼是::CodeIgniter登錄系統使用md5()

視圖/ login_form.php

<?php 
     // form design  

    echo form_open("login/validate") . "<br/>";  
    echo form_input("username", "Username") . "<br/> <br/>"; 
    echo form_password("password", "password") . "<br/> <br/>"; 
    echo form_submit("submit", "Login") . "<Br/> <Br/>"; 

    echo anchor("login/sign_up", "Create Account"); 

?> 

控制器/ login.php中

<?php 
    class Login extends CI_Controller { 

     function index() { // function of loading login page 
      $data['main_content'] = "login_form"; // name of login page 

      $this->load->view("includes/templet", $data); 
     } 

    function validate() { // form validation   
     $this->load->model("membership_model"); // load "membership_model" 
     $query = $this->membership_model->validate_user(); // check validation 

     if($query) {  // if data found 
      $data = array( // value which is to be inserted into session 
       "username" => $this->input->post("username"), 
       "is_logged_in" => true 
      ); 

      $this->session->set_userdata($data); // insert value into session 
      redirect("site/members_area"); // go to predefined page 
     } 
     else { 
      // if data not found then go to login_form page 
      $this->index(); 
     } 
    } 

    // create account 
    function sign_up() { 
     $data['main_content'] = "sign_up"; 

     $this->load->view("includes/templet", $data); 
    } 

    // insert data into database 
    function create_member() { 

     $this->load->library("form_validation"); // load form_validation library 

     $this->form_validation->set_rules("username", "Username", "trim|required"); 
     $this->form_validation->set_rules("password", "Enter Password", "trim|required|min_length[4]|max_length[32]"); 
     $this->form_validation->set_rules("re_password", "Confirm password", "trim|required|matches[password]"); 

     $this->form_validation->set_rules("email", "Email Address", "trim|required|valid_email"); 

     if( $this->form_validation->run() == FALSE) { 
      $this->load->view("sign_up"); 
     } 
     else { 
      $this->load->model("membership_model"); 

      if($query = $this->membership_model->create_member()) { 
       // redirect("site/members_area"); 
       $data['main_content'] = "signup_success"; 

       $this->load->view("includes/templet", $data); 
      } 
      else { 
       $this->load->view("sign_up"); 
      } 
     } 
     } 
    } 

?> 

模型/ membership_model

<?php 

    class Membership_model extends CI_Model { 
     function validate_user() { 
      $this->db->where('username', $this->input->post('username')); 
      $this->db->where('password', md5($this->input->post('password'))); 

      $query = $this->db->get("user"); 

      if($query->num_rows == 1) { 
       return true; 
      } 
     } 

     function create_member() { 
      $new_member_insert_data = array(     
       'username' => $this->input->post("username"), 
       'password' => md5($this->input->post("password")), 
       'email' => $this->input->post("email")     
     ); 

      $insert = $this->db->insert("user", $new_member_insert_data); 

      return true; 
     } 
    }  

?> 
+4

不要使用MD5口令存儲它是不安全的。使用[Bcrypt](http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php)。也不要試圖重新發明輪子。 CodeIgniter有許多同行評審的認證庫,它們會更安全,更容易設置。有關更多信息,請參閱http://stackoverflow.com/questions/346980/how-should-i-choose-an-authentication-library-for-codeigniter。 –

+0

1.你到底有什麼問題? 2.我不明白你真的想用'md5()',或者你想用它嗎? –

回答

7

我認爲你表中的列「Password」小於varchar(32),md5要求 varchar類型。

1

是md5密碼 檢查數據庫領域,由於MD5生成32個charactors ..所以更改表字段爲varchar(32)

$data=array( 
      'password' => md5($this->input->post('password')), 
     ); 
-1

剛剛從

if($query->num_rows == 1) 
改變這一聲明

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

row()

模型/ membership_model.php

它的工作原理