【问题标题】:Codeigniter userdata session is emptyCodeigniter 用户数据会话为空
【发布时间】:2014-11-01 13:43:04
【问题描述】:

当我使用 set_userdata 设置会话并检查其他页面时,除了 userdata 之外,一切都设置得很好...... 我使用var_dump($this->session->all_userdata()); 和代码说

array(5) { ["session_id"]=> 字符串(32) "7195e5aea1695762492bf85d44112120" ["ip_address"]=> 字符串(13) "xxx.xxx.x.xxx" ["user_agent"]=> 字符串(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.3" ["last_activity"]=> int(1414848680) ["user_data"] => 字符串(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');
        }
    }
}

和应用程序/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;?>

和应用程序/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-&gt;load-&gt;view('nav.php',$data); 将数据发送到视图但它不起作用......)

【问题讨论】:

  • 我已经测试了你的代码并且没问题。尝试调试它有什么问题。write var_dump($this->session->userdata); die() 在 $this->session->set_userdata($new_data) 行之后;并检查它是否设置了您的会话数据。如果没有,那么问题可能出在您查询它没有获取数据的位置。
  • 我也面临这个问题,在我成功登录后的情况下,我在会话中设置 user_data 并在回显整个会话数据之后,这很好,但在重新加载页面后 user_data 变量显示空值.. ..您对此有任何解决方案吗....?

标签: php codeigniter session user-data


【解决方案1】:

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

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

你可以使用

redirect(main);

重定向服务器端

【讨论】:

    【解决方案2】:

    您正在加载会话类吗?

    $this->load->library('session');

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

    【讨论】:

      【解决方案3】:

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

      $config['cookie_secure']  = TRUE
      

      and 'cookie_secure' = 只有存在安全的 HTTPS 连接时才会设置 Cookie。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多