简体   繁体   English

Codeigniter用户数据会话为空

[英]Codeigniter userdata session is empty

When I set the session using set_userdata and check that at the other page, everything is setted well excepting userdata... I use var_dump($this->session->all_userdata()); 当我使用set_userdata设置会话并在另一页上进行检查时,除了userdata之外,其他所有设置都设置正确...我使用var_dump($this->session->all_userdata()); and code says 和代码说

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, like Gecko) Chrome/38.0.2125.104 Safari/537.3" ["last_activity"]=> int(1414848680) ["user_data"]=> string(0) "" } array(5){[“ session_id”] =>字符串(32)“ 7195e5aea1695762492bf85d44112120” [“ ip_address”] =>字符串(13)“ xxx.xxx.x.xxx” [“ user_agent”] =>字符串(120) “ Mozilla / 5.0(Macintosh;英特尔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”] =>字符串(0)“”}

user_data part is empty. user_data部分为空。

Here is my code (application/controllers/login.php) 这是我的代码(application / controllers / 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');
        }
    }
}

and application/views/nav.php 和应用程序/视图/ 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;?>

and application/config/config.php 和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

Please! 请! help me! 帮我! This error makes me crazy! 这个错误使我发疯! Please...! 请...! (I try to solve this calling userdata in controller and send data to the view using $this->load->view('nav.php',$data); But it didn't work...) (我尝试解决在控制器中调用此userdata的问题,并使用$this->load->view('nav.php',$data);将数据发送到视图$this->load->view('nav.php',$data);但这没有用...)

are you printing the userdata in the view or the controller? 您是否在视图或控制器中打印用户数据? Try printing it in the controller. 尝试在控制器中打印。 I feel in the view the $this is not the same as in the controller which could be the cause of the issue. 我认为$ this与控制器中的$ this不同,这可能是问题的原因。 You may have to pass $new_data to the view. 您可能必须将$ new_data传递给视图。 In addition instead of 另外代替

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

you could use 你可以用

redirect(main);

which redirects server side 重定向服务器端

Are you loading the session class? 您正在加载会话课程吗?

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

I always set session in the autoload. 我总是在自动加载中设置会话。 But when I forget I run in this kind of problems. 但是,当我忘记我遇到这种问题时。

I may be wrong because I do not know where your code is running, but if you are localhost you may find browsers refusing your cookie. 我可能是错的,因为我不知道您的代码在哪里运行,但是如果您是localhost,则可能会发现浏览器拒绝您的cookie。 also you have 你也有

$config['cookie_secure']  = TRUE

and 'cookie_secure' = Cookies will only be set if a secure HTTPS connection exists. and'cookie_secure'= Cookies仅在存在安全HTTPS连接的情况下设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM