Codeigniter userdata会话为空

时间:2014-11-01 13:43:05

标签: php codeigniter session user-data

当我使用set_userdata设置会话并在另一页检查时,除了userdata之外,所有内容都设置得很好... 我使用var_dump($this->session->all_userdata());,代码说

  

array(5){[" session_id"] => string(32)" 7195e5aea1695762492bf85d44112120" [" IP_ADDRESS"] => string(13)" xxx.xxx.x.xxx" [" USER_AGENT"] => string(120)" Mozilla / 5.0(Macintosh; Intel Mac OS X 10_10_0)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 38.0.2125.104 Safari / 537.3" [" last_activity"] => int(1414848680)[" user_data"] => string(0)"" }

user_data部分为空。

这是我的代码 (应用/控制器/ login.php中)

class Login extends CI_Controller {
    function index(){
        $this->load->model('Join_model');
        $this->load->view('head');
        if($this->input->post('id')){
            $data = $this->Join_model->gets(array(
               'table'=>'acc_wait_tb',
               'id'=>$this->input->post('id'),
               'password'=>$this->input->post('password'),
            ));
            if($data == true){
                $dd = $this->db->get_where('acc_wait_tb', array('id'=>$this->input->post('id')))->row();
                $new_data = array(
                    'user_id'=>$dd->id,
                    'name'=>$dd->name,
                    'email'=>$dd->email,
                );
                $this->session->set_userdata($new_data);
                echo "<script>
                    location.href='/index.php/main';
                    </script>";
            }else if($data == false){
                echo "<script>alert('Login Fail.');</script>";
                $this->load->view('log_main');
                $this->load->view('footer');
            }
        }else{
            $this->load->view('log_main');
            $this->load->view('footer');
        }
    }
}

和application / views / nav.php

var_dump($data);
$user_id = $this->session->userdata('user_id');?>
<?php if($user_id != ""): ?>
<p class="navbar-text pull-right" style="margin-right:30px;">Hi,
  <a class="navbar-link" href=""><?=$this->session->userdata('name')?></a>
</p>
<?endif;?>

和application / config / config.php

$config['encryption_key'] = 'asdfqwerertydfghcvbnsdfgqwerxcvb';

/*
|--------------------------------------------------------------------------
| Session Variables
|--------------------------------------------------------------------------
|
| 'sess_cookie_name'    = the name you want for the cookie
| 'sess_expiration'     = the number of SECONDS you want the session to last.
|   by default sessions last 7200 seconds (two hours).  Set to zero for no expiration.
| 'sess_expire_on_close'  = Whether to cause the session to expire automatically
|   when the browser window is closed
| 'sess_encrypt_cookie'   = Whether to encrypt the cookie
| 'sess_use_database'   = Whether to save the session data to a database
| 'sess_table_name'     = The name of the session database table
| 'sess_match_ip'     = Whether to match the user's IP address when reading the session data
| 'sess_match_useragent'  = Whether to match the User Agent when reading the session data
| 'sess_time_to_update'   = how many seconds between CI refreshing Session Information
|
*/
$config['sess_cookie_name']   = 'ci_session';
$config['sess_expiration']    = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']  = TRUE;
$config['sess_table_name']    = 'ci_sessions';
$config['sess_match_ip']    = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update']  = 300;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a 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' =  Cookies will only be set if a secure HTTPS connection exists.
|
*/
$config['cookie_prefix']  = "";
$config['cookie_domain']  = "";
$config['cookie_path']    = "/";
$config['cookie_secure']  = TRUE

请!帮我!这个错误让我发疯!请...! (我尝试在控制器中解决此调用userdata并使用$this->load->view('nav.php',$data);将数据发送到视图但是它没有工作......)

3 个答案:

答案 0 :(得分:0)

您是在视图或控制器中打印用户数据吗?尝试在控制器中打印它。我觉得在视图中$ this与控制器中的不同可能是导致问题的原因。您可能必须将$ new_data传递给视图。另外代替

echo "<script>
location.href='/index.php/main';
</script>";

你可以使用

redirect(main);

重定向服务器端

答案 1 :(得分:0)

您正在加载会话类吗?

$这 - &GT;负载&GT;库(&#39;会话&#39);

我总是在自动加载中设置会话。但是当我忘记我遇到这种问题时。

答案 2 :(得分:0)

我可能错了,因为我不知道你的代码在哪里运行,但如果你是localhost,你可能会发现浏览器拒绝你的cookie。你也有

$config['cookie_secure']  = TRUE

和&#39; cookie_secure&#39; =只有存在安全的HTTPS连接时才会设置Cookie。