MongoDB登录表单不起作用

时间:2015-06-27 16:45:05

标签: php mysql mongodb codeigniter

我正在尝试将我的CodeIgniter项目从MySQL更改为MongoDB数据库以比较这两者。我在寄存器表单上遇到了一些问题,但它现在工作正常,但是现在,我无法登录。我尝试了很多不同的东西,但它仍然不起作用。请帮忙!

这是MySQL登录/注册文件:

controllers / users.php(MySQL方式)

<?php
class Users extends CI_Controller{
    public function register(){
        $this->form_validation->set_rules('first_name','First Name','trim|required|max_length[50]|min_length[2]|xss_clean');
        $this->form_validation->set_rules('last_name','Last Name','trim|required|max_length[50]|min_length[2]|xss_clean');
        $this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');
        //trim usuwa biale znaki
        if($this->form_validation->run() == FALSE){
            $data['main_content']='users/register';
            $this->load->view('layouts/main',$data);    
        }else{
            if($this->User_model->create_member()){ //w config autoload ustawione autoload model
                $this->session->set_flashdata('registered','Zarejestrowano pomyślnie');
                redirect('home/index'); 
            }
        }
    }


//user model login_user

    public function login(){
        $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');      
        $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');        

        if($this->form_validation->run() == FALSE){
            //Złe dane - error
            $this->session->set_flashdata('login_failed', 'SORY, taka nazwa użytkownika nie istnieje. ');
            redirect('home/index');
        } else {
           //Pobierz z posta 
           $username = $this->input->post('username');
           $password = $this->input->post('password');

           //Pobierz id uzytkownika z User modela
           $user_id = $this->User_model->login_user($username,$password);

           //Walidacja
           if($user_id){
               //Tworzy array danych uzytkownika
               $user_data = array(
                       'user_id'   => $user_id,
                       'username'  => $username,
                       'logged_in' => true
                );
                //Tworzy nową sesje userdata
               $this->session->set_userdata($user_data);

               redirect('home/index');
            } else {
                //Blad logowania
                $this->session->set_flashdata('login_failed', 'SORY, taka nazwa użytkownika nie istnieje. ');
                redirect('home/index');
            }
        }
    }


    public function logout(){
        //Unset user data
        $this->session->unset_userdata('logged_in');
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('username');
        $this->session->sess_destroy();
        //cos sie wali z flashdata i nie wyswietlalo wylogowany
        redirect('home/index');
    }
}

models / user_model.php(MySQL方式)

<?php

class User_model extends CI_Model{
    public function create_member(){
        //input helper dla post $this->input->post()
    $enc_password = md5($this->input->post('password')); //ukrywanie hasla

    $data=array(
    'first_name'    =>$this->input->post('first_name'),
    'last_name'     =>$this->input->post('last_name'),
    'email'         =>$this->input->post('email'),
    'username'      =>$this->input->post('username'),
    'password'      =>$enc_password
    );

    //active record class this-db-get
    //array $data jest przekazywany insert do db
    $insert =  $this->db->insert('users',$data);
    return $insert;
    //if insert - user model w users.php success 

    }
    public function login_user($username,$password){

        $enc_password = md5($password);

    //porownywanie z danymi z db
    //$this->db->where('username',$username);
    //$this->db->where('password',$enc_password);

$collection = $this->db->users;
$cursor = $collection->find(array("username" => $username, "password" => $enc_password));
$result = $cursor->count();


    if($result==1){
        //0 - pierwszy z arraya
        return $result->cursor(0)->id;

    }else{
        return false;
    }
    }

}

现在有我的MongoDB登录/注册文件:

controllers / users.php(MongoDB方式)

<?php
class Users extends CI_Controller{

    private $database;

    public function __construct(){
        parent::__construct();
        $m = new MongoClient(); 
        $db = $m->test;

        $this->database=$db;
    }

    public function register(){

        $this->form_validation->set_rules('first_name','First Name');
        $this->form_validation->set_rules('last_name','Last Name');
        $this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
        $this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
        $this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');


        if($this->form_validation->run() == FALSE){
            $data['main_content']='users/register';
            $this->load->view('layouts/main',$data);    
        }else{

            if($this->User_model->create_member()){ //w config autoload ustawione autoload model
                $this->session->set_flashdata('registered','Zarejestrowano pomyślnie');
                redirect('home/index'); 
            }
        }
    }

//user model login_user

    public function login(){
        //$this->load->model('user_model');
        $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');      
        $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');        

        $collection = $this->database->users;

        if($this->form_validation->run() == FALSE){
            //Złe dane - error
            $this->session->set_flashdata('login_failed', 'SORY, taka nazwa użytkownika nie istnieje. ');
            redirect('lists');
        } else {
           //Pobierz z posta 

          $username = $this->input->post('username');
          $password = $this->input->post('password');

           //Pobierz id uzytkownika z User modela
           $user_id = $this->User_model->login_user($username,$password);

           //Walidacja
           if($user_id){
               //Tworzy array danych uzytkownika
               $user_data = array(
                       'user_id'   => $user_id,
                       'username'  => $username,
                       'logged_in' => true
                );
                //Tworzy nową sesje userdata
               $this->session->set_userdata($user_data);

               redirect('lists');
            } else {
                //Blad logowania
                $this->session->set_flashdata('login_failed', 'SORY, taka nazwa użytkownika nie istnieje. ');
                redirect('lists');
            }
        }
    }

    public function logout(){
        //Unset user data
        $this->session->unset_userdata('logged_in');
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('username');
        $this->session->sess_destroy();
        //cos sie wali z flashdata i nie wyswietlalo wylogowany
        redirect('home/index');
    }
}

models / user_model.php(MongoDB方式)

<?php

class User_model extends CI_Model{

    private $database;

    public function __construct(){
        parent::__construct();
        $m = new MongoClient(); 
        $db = $m->test;

        $this->database=$db;
    }

    public function create_member(){
        //input helper dla post $this->input->post()


    $enc_password = md5($this->input->post('password'));

    $data=array(
    'first_name'    =>$this->input->post('first_name'),
    'last_name'     =>$this->input->post('last_name'),
    'email'         =>$this->input->post('email'),
    'username'      =>$this->input->post('username'),
    'password'      =>$enc_password
    );

    $collection = $this->database->users;
    /*
            $first_name="Przyk�adowe_imie";
            $last_name="Przyk�adowe_opis";
            $email="Przyk�adowy_email";
            //$telefon=$i;
            $username="Przykladowy_main";
            $password="Przykladowe haslo";
            $document = array( "first_name" => $first_name, "last_name" => $last_name, "email"=>$email, "username"=>$username,  "password"=>$password);
            $collection->insert($document);
    */


    //active record class this-db-get
    //array $data jest przekazywany insert do db

    //$collection->insert();
    $collection->insert($data);
        return $collection;

    $collection2 = $this->database->family; 

    $document=array(
    'list_name'             =>$this->input->post('list_name'),
    'list_body'             =>$this->input->post('list_body'),
    'list_user_id'          =>$this->input->post('list_user_id'),
    'create_date'           =>$this->input->post('create_date')
    );
    /*
    $list_name="list_name1";
    $list_body="list_body1";
    $list_user_id="list_user_id1";
    $create_date="create_date";
    */
        //$document = (array("list_name"=>$list_name, "list_body"=>$list_body,"list_user_id"=> $list_user_id, "create_date"=>$create_date));
        $collection2->insert($document);    
        return $collection2;




    //$insert =  $this->db->insert('users',$data);
    ///return $insert;
    //if insert - user model w users.php success 

    }
    public function login_user($username,$password){

    $collection = $this->database->users;   
    $enc_password = md5($password);

    //porownywanie z danymi z db
    $this->db->where('username',$username);
    $this->db->where('password',$enc_password);

    $result = $this->db->get('users');
    if($result->num_rows()==1){
        //0 - pierwszy z arraya
        return $result->row(0)->id;

    }else{
        return false;
    }
    }
}

没有显示错误,只是我无法登录。用户正确地在数据库中注册,具有注册表单。

0 个答案:

没有答案