3

I am new in CodeIgniter and using CodeIgniter_2.1.3. I am following NETTUTS CodeIgniter tutorial. I want to develop a login system with codeIgniter which works well without md5() function [without encrypted password] . Then I create new table with encrypted password using md5() function. Then login code doesn't work. My code is ::

view/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"); 

 ?>

controllers/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");
            }
        }
      }
    }

 ?>

model/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

3 回答 3

7

我认为您表中的“密码”列小于varchar(32)md5需要32 个varchar 类型。

于 2013-06-28T10:00:15.703 回答
3

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

$data=array( 
            'password' => md5($this->input->post('password')),
          );
于 2015-05-08T04:52:57.633 回答
0

只需将此语句从

if( $query->num_rows == 1 )

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

row()你的

模型/membership_model.php

有用

于 2018-02-28T10:04:45.213 回答