使用md5()的CodeIgniter登录系统

时间:2013-06-27 19:44:10

标签: codeigniter

我是 CodeIgniter 的新手,并使用 CodeIgniter_2.1.3 。我正在关注 NETTUTS CodeIgniter 教程。我想开发一个带有codeIgniter的登录系统,该系统在没有 md5()功能[没有加密密码]的情况下运行良好。然后我使用 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;
      }
   }     

 ?>

3 个答案:

答案 0 :(得分:7)

我认为表格中的“密码”列小于 varchar(32) md5 需要 32 varchar类型。< / p>

答案 1 :(得分:2)

对于md5密码 检查数据库字段,因为md5生成32个字符..所以将表字段更改为varchar(32)

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

答案 2 :(得分:0)

只需从

更改此声明即可
if( $query->num_rows == 1 )

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

中的row()
  

模型/ membership_model.php

它有效