在重定向后,codeigniter中的会话无法正常工作

时间:2017-05-01 16:29:14

标签: php codeigniter session

我正在尝试在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.";
            }
重定向到此页面后

。我得到alwasy会话没有设置。为什么会这样?

会话和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;

2 个答案:

答案 0 :(得分:0)

localhost更改为127.0.0.1有效。如果你知道这个问题背后的原因,那么就会有一种享受。

答案 1 :(得分:-1)

我的代码

//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');
        }

    }