如何使用codeigniter在登录表单中设置会话

时间:2015-11-06 09:16:28

标签: php codeigniter

我是codeigniter的新手。我无法在登录表单中设置会话。

我的模型页面是user.php:

import os, subprocess
root = r"c:\myfloder"
minifier = '"c:\yuicompressor-2.4.7.jar"'
for path, subdirs, files in os.walk(root):
    for name in files:
         fullFile = os.path.join(path, name)
         if name.endswith(".js"):
            minifyCommand = ' --disable-optimizations '+'"'+fullFile+'"' +" -o "+'"'+fullFile+"min"+'"'
            subprocess.call(["java","-jar",minifier,minifyCommand])
print "done"

我的控制器页面是login1.php:

<?php
class User extends CI_Model

{

    function __construct()
{

        parent::__construct();

        $this->load->database();

        $this->load->library('session');

        $this->load->helper('url');
    }

    public function login_model($username,$password)
{

         $where = array('username'=>$username, 'password'=>$password);

            $query = $this->db->get_where('signtable', $where);

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

                 $row = $query->row();

                        $data = array(
                                  'user_logged_in'  =>  TRUE,
                                  'password' => $row->password,
                                  'username' => $row->username);

                            $this->session->set_userdata($data);

                                }
                              else

                                     {


                echo "invalid username or password";

            }


             }

    }


?>

我的其他控制器页面是login2.php

<?php
class Login1 extends CI_Controller
{

    function __construct()
{
        parent::__construct();
        $this->load->database();
        $this->load->model('User');
        $this->load->helper('form');
    }
    public function index()
{
       if($this->input->post())
{
       $this->load->library('form_validation');

$this->form_validation->set_rules('name','Username','required|callback_username_check|trim|xss_clean');
            $this->form_validation->set_rules('pass','Password','required|callback_password_check|trim');

            if ($this->form_validation->run() == FALSE)
{
                    $this->load->view('login');
    }
            else
            {

            $username =  $this->input->post('name');
                        $password =  $this->input->post('pass');
                      $this->User->login_model($username,$password);

                       } 
   }
       $this->load->view('login');
    }
    }
    ?> 

我的会话控制页面是session_ctrl.php:

<?php
     class Login2 extends CI_Controller
{
    function __construct()
{
     parent::__construct();
        $this->load->database();
        $this->load->model('User');
        $this->load->helper('url');
        }
     public function index ()
{
           $this->load->model('Retrieve_model');
          $data['result'] = $this->Retrieve_model->retrieve();
          $this->load->view('logout',$data);
}
     }
?>

我的登录视图页面是login.php:

<?php
class Session_ctrl extends CI_Controller
{
    public function __construct()
{
             parent::__construct();
             $this->load->library('session');
             $this->load->helper('url');
             $this->load->database();
             $this->load->model('User');
            }
       public function index()
{
        if($this->User->login_model($username,$password))
                         {
                                    redirect('login2','refresh');           
                         }  
                         else 
                         {
                          redirect('login1','refresh');
                              }
                         $this->load->view('login');
                  }
                  }
      ?> 

我的注销视图页面是logout.php:

<?php  
echo validation_errors();
echo form_open('login1');
?>
<html>
    <body>
        <tr>
        <td>Username <input type="text" name="name" value = "<?php echo set_value('name'); ?>"></td><td><?php echo  form_error("name");?></td><br><br>
        <td>Password <input type="password" name="pass" ></td><td><?php echo form_error("pass");?></td><br><br>
        <input type="submit" name="submit" value ="LogIn">
        <?php form_close(); ?>
        </tr></body>
    </html>

2 个答案:

答案 0 :(得分:1)

尝试在方法中加载会话

public function login_model($username,$password)
{
    $this->load->library('session'); # Load session

    $where = array('username'=>$username, 'password'=>$password);

    $query = $this->db->get_where('signtable', $where);

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

            $row = $query->row();

            $data = array(
                    'user_logged_in'  =>  TRUE,
                    'password' => $row->password,
                    'username' => $row->username);

                    $this->session->set_userdata($data);

        }else{


            echo "invalid username or password";

        }


}

以这种方式召集会议 -

$this->session->userdata('username');

答案 1 :(得分:0)

在控制器中

public function index()
{

    $this->load->library('form_validation');

    $this->form_validation->set_rules('name','Username','required|callback_username_check|trim|xss_clean');
    $this->form_validation->set_rules('pass','Password','required|callback_password_check|trim');

    if ($this->form_validation->run() == FALSE){
        echo "Invalid User Loging";
        # $this->load->view('login');
    }
    else{

        $username   =  $this->input->post('name');
        $password   =  $this->input->post('pass');
        $result     = $this->model_name->check_login($username,$password);

        if ($result != FALSE ) {
            # Inavlid User...
            $this->load->view('login');
        }
        else{
            # Valid user
            $this->load->view('home');
        }
    }   
}

在模型中

public function check_login($username,$password)
{

    $query = $this->db->query("SELECT * FROM signtable WHERE username = '$username' AND password = '$password' ")
    $result = $query->result_array();
    $count =count($result);


    if(empty($count) || $count >1){
        return FALSE;
    }
    else{

        $data = array(
            'user_logged_in'  =>  TRUE,
            'username' => '$username'
            );

        $this->session->set_userdata($data);
    }
}

表格打开应

echo form_open('controller_name/index');