【问题标题】:session in codeigniter not working after redirect重定向后codeigniter中的会话不起作用
【发布时间】:2018-03-26 22:59:25
【问题描述】:

我正在尝试在 codeigniter 中使用会话。我做了什么:

if ( verifyHashedPassword($this->input->post('teacher_password'),$result['teacher_password']))
                    {
                        $this->session->set_userdata('teacher_email',$this->input->post('teacher_email'));
                        $responseArray['success'] = true;
                    }

我正在尝试使用:

if ( $this->session->userdata('teacher_email') )
            {
                echo "session is set.";
            }
            else
            {
                echo "session is not set.";
            }

重定向到此页面后。我总是没有设置会话。为什么会这样?

会话和cookie配置是:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix'   = Set a cookie name prefix if you need to avoid collisions
| 'cookie_domain'   = Set to .your-domain.com for site-wide cookies
| 'cookie_path'     = Typically will be a forward slash
| 'cookie_secure'   = Cookie will only be set if a secure HTTPS connection exists.
| 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript)
|
| Note: These settings (with the exception of 'cookie_prefix' and
|       'cookie_httponly') will also affect sessions.
|
*/
$config['cookie_prefix']    = '';
$config['cookie_domain']    = '';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

【问题讨论】:

  • 你在两个页面进程中都加载会话类吗?
  • 你的意思是会话库?我正在将它加载到我的控制器构造函数中。
  • 在设置会话数据之前回显$this->input->post('teacher_email')。也许,它是空的
  • 不是我检查过,验证过。
  • 你的问题和我刚才在stackoverflow.com/questions/43720370/…解释的一样,不是说没有其他问题,先解决$config['sess_save_path']吧。

标签: php codeigniter session


【解决方案1】:

localhost 更改为127.0.0.1 有效。如果您知道问题背后的原因,我们会为您提供款待。

【讨论】:

    【解决方案2】:

    这是我的代码

    //cek login
    public function index()
        {
            if ($this->session->userdata('id_jurusan') ==1) {
                $where=array('id_jurusan'=>$this->session->userdata('id_jurusan'));
                $value2 = $this->session->userdata('username');
                $data['riwayat_rule'] = $this->guru_mod->get_data_all2('forward_changing','username',$value2);
                $data['user']=$this->guru_mod->get_where($where,'user')->result();
                $this->load->view('guru/index',$data);
            } else if ($this->session->userdata('id_jurusan') ==2) {
                $where=array('id_jurusan'=>$this->session->userdata('id_jurusan'));
                $data['user']=$this->guru_mod->get_where($where,'user');
                $this->load->view('guru/index',$data);
            }else{
                redirect('login');
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 1970-01-01
      • 2015-09-15
      • 2013-08-13
      • 2014-11-19
      • 2013-08-11
      • 2013-10-06
      • 2016-12-01
      相关资源
      最近更新 更多