codeigniter中的会话无法正常工作

时间:2018-05-30 04:57:57

标签: php codeigniter-3

会话无法正常运行。登录时,如果条件不起作用,则指示member_area函数是正确的但在member_area函数中。结果是直接进入member_area函数的else部分,即输出die。如果条件,在member_area函数中是否有错误?

这是我的主控制器,即欢迎控制器

 function login()
    {
        $this->form_validation->set_rules('email', 'E-mail', 'valid_email');
        $this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
            if ($this->form_validation->run()==FALSE)
           {
             $data['view']  = 'login_view';
             $this->load->view('load_view',$data);

           }
        else
        {           

                 $email=$_POST['email'];
                 $password = md5($_POST['password']);
                 $this->load->model('Sample_model');
                 $credentials = array(
                                    'email' => $_POST['email'],
                                    'password' => md5($_POST['password'])
                                       );
                  $user = $this->Sample_model->check_user($credentials);
                  if($user->num_rows() == 1)
                  {

                    $user = $user->row();
                   $session = array(
                                 'name' => $user->name,
                                 'is_logged_in' => TRUE
                                  );
                   $this->session->set_userdata($sesson);
                   redirect('welcome/member_area');

                  }

                       else {

                             $data['view'] = 'error_view';
                              $data['msg']  = 'Login failed';
                              $this->load->view('load_view',$data);

                           }


        }

    }
     public function member_area()
    {
       if($this->session->userdata('is_logged_in'))
       {  
        $data['view'] = 'memberarea_view';
        $this->load->view('load_view',$data);
      }

      else  die('die');

     }

I have added session in config/Autoload

$autoload['libraries'] = array('database','session','form_validation');

这是本登录中使用的模型。  Sample_model

<?php
class Sample_model extends CI_Model {

        public function __construct()
        {
                //Call the CI_Model constructor
                parent::__construct();
        }



        public function add_user($user)
        {
            return $this->db->insert('users', $user);
        }
     public function check_user($credentials)
       {
         $this->db->where($credentials);
         return $this->db->get('users');
       }

}

这是login_view表单。我正在从此表单输入电子邮件和密码。

<section class="title">
    <div class="container">
      <div class="row-fluid">
        <div class="span6">
          <h1>Login</h1>
        </div>
        <div class="span6">
          <ul class="breadcrumb pull-right">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li><a href="#">Pages</a> <span class="divider">/</span></li>
            <li class="active">Login</li>
          </ul>
        </div>
      </div>
    </div>
  </section>
  <!-- / .title -->       


  <section id="login-page" class="container">
    <form class="center" action='' method="POST">
      <fieldset class="login-form">

        <div class="control-group">
          <!-- E-mail -->
          <div class="controls">
            <input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
             <?php echo form_error('email'); ?>
          </div>
        </div>

        <div class="control-group">
          <!-- Password-->
          <div class="controls">
            <input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
             <?php echo form_error('password'); ?>
          </div>
        </div>



        <div class="control-group">
          <!-- Button -->
          <div class="controls">
            <button class="btn btn-success btn-large btn-block">Login</button>
          </div>
        </div>
      </fieldset>
    </form>
  </section>
  <!-- /#login-page -->

1 个答案:

答案 0 :(得分:0)

$ autoload ['libraries'] = array('database','session','form_validation');

辅助

$ autoload ['helper'] = array('url','file');

控制器

已定义('BASEPATH')或退出('不允许直接访问脚本');

类Welcome扩展了CI_Controller {

function login()
{

    $this->form_validation->set_rules('email', 'E-mail', 'valid_email');
    $this->form_validation->set_rules('password', 'Password', 'min_length[5]|max_length[8]');
        if ($this->form_validation->run()==FALSE)
       {
         $data['view']  = 'login_view';
         $this->load->view('login_view',$data);

       }
    else
    {           

             $email=$_POST['email'];
             $password = md5($_POST['password']);
             $this->load->model('Sample_model');
             $credentials = array(
                                'email' => $_POST['email'],
                                'password' => md5($_POST['password'])
                                   );
              $user = $this->Sample_model->check_user($credentials);
              if($user->num_rows() == 1)
              {

                $user = $user->row();
               $session = array(
                             'name' => $user->name,
                             'is_logged_in' => TRUE
                              );
               $this->session->set_userdata($session);
               redirect('welcome/member_area');

              }

                   else {

                         $data['view'] = 'error_view';
                          $data['msg']  = 'Login failed';
                          $this->load->view('login_view',$data);

                       }


    }

}



 public function member_area()
{  
   if($this->session->userdata('is_logged_in'))
   {  
    $data['view'] = 'member_view';

    $this->load->view('member_view',$data);
  }
  else{  die('die');
     }

 }

    function logout()
    {
    $newdata = array(
    'name'  =>'',              
    'is_logged_in' => FALSE,
    );

    $this->session->unset_userdata($newdata);
    $this->session->sess_destroy();

    redirect('welcome/login','refresh');
    }

}

模型

class Sample_model扩展了CI_Model {

    public function __construct()
    {
            //Call the CI_Model constructor
            parent::__construct();
    }



    public function add_user($user)
    {
        return $this->db->insert('users', $user);
    }
 public function check_user($credentials)
   {
     $this->db->where($credentials);
     return $this->db->get('users');
   }

}

登录视图

      

    <div class="control-group">
      <!-- E-mail -->
      <div class="controls">
        <input type="text" id="email" name="email" placeholder="E-mail" class="input-xlarge" required="required">
         <?php echo form_error('email'); ?>
      </div>
    </div>

    <div class="control-group">
      <!-- Password-->
      <div class="controls">
        <input type="password" id="password" name="password" placeholder="Password" class="input-xlarge" required="required">
         <?php echo form_error('password'); ?>
      </div>
    </div>



    <div class="control-group">
      <!-- Button -->
      <div class="controls">
        <button class="btn btn-success btn-large btn-block">Login</button>
      </div>
    </div>
  </fieldset></form></section>

会员视图

欢迎会员