doufan6886 2015-07-30 14:36
浏览 53

如何在控制器中传递会话

I have a problem about session in CodeIgniter. I initialize a session in Login/submit Then , i have to check in another controller if the user is logged. So i would use:

if ( $this->session->userdata('login_state') == FALSE ) {
      $this->load->view('header');    // no session established, kick back to login page
     }

The problem is that in the controller in which i would check the login_state , i can't use the $this->session variable. If i load the session library in this controller , i'll initialize a new session and the $this->session->userdata('login_state') will be always false. I tried the :

   $ci = & get_instance();
   $ci->session->userdata('login_state');

but it doesn't work.I don't know why.. I would not create a class to share the session variable. is there a clear way to pass the session variable through controllers?

Login controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
  public function submit() {
            $this->load->library('session');
    // Prendo i dati passati dal form
      $username = $this->input->post('username');
      $password = $this->input->post('password');

    // read user's credentials from db, through Login Model
      if ( $username == "my_username"  && $password == "my_password" )  {
        $this->session->set_userdata('login_state', TRUE);
      } 
      else {
        redirect( '/' );    // redirect back to login page
      }
  }

ragazze controller

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ragazze extends CI_Controller {    
    public function search()
    {   

    if ( $this->session->userdata('login_state') == FALSE ) {
      $this->load->view('header');    // no session established, kick back to login page
     }
     else{
        $this->load->model('ragazza_model');
        #Prelevo il valore passato tramite post
        $nome_ragazza=$this->input->post('nome');
        if ($nome_ragazza) {
        $nome_ragazza = $this->ragazza_model->get_girl_by_name($nome_ragazza);
        }
        $data['nome_ragazza'] =$nome_ragazza;
        $this->load->helper('form');
        $this->load->model('ragazza_model');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nome', 'Nome', 'required');

        if ($this->form_validation->run() == FALSE){
        //Questo viene eseguito anche quando il form
        //non `e compilato, cio´e la prima volta che si  -
        //la pagina con il form html
          $this->load->view('header');
          $this->load->view('menu');
          $this->load->view('ragazze/search',$data);
          $this->load->view('footer');
        }
        else{
        //Se i controlli sono positivi, redireziono l' -
          $this->load->view('header');
          $this->load->view('menu');
          $this->load->view('ragazze/search',$data);
          $this->load->view('footer');
        }
       }
       }

config.php

 $config['sess_cookie_name']        = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

EDIT: ok i discovered somethin interesting. If i use Explorer..everythings works..So the problem is Firefox..i don't know Why. Can you help?

EDIT: I tried a lot of things.. but at the end mybe it was a cookie problem of my firefox. I deleted oll my cookies and now the session seems to work. Really , i don't know if the problem was this. I changed also somethings in the config but before to delete my cookies it didin't work. So guys , if you have my same problem with Firefox ( cause in Explorer the session, so the same code, worked) try just to delete your cookies. If someone has an answer to this strange thing, i'll appreciate it.

  • 写回答

1条回答 默认 最新

  • douliandan7340 2015-07-30 15:34
    关注

    In your autoload.php config file, auto load the session like this:

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

    Now you don't load it again, and you don't "pass" session variables. They are simply available for use everywhere.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能