当我使用set_userdata设置会话并在另一个页面检查时,除了userdata之外,一切都设置得很好。。。我使用var_dump($this->session->all_userdata());
,代码显示
array(5){["session_id"]=>string(32)"7195E5ea1695762492bf85d44112120"["ip_address"]=>string(13)"xxx.xxx.x.xxxx"["user_agent"]=>string(120)"Mozilla/5.0(Macintosh;Intel Mac OS x 10_10_0)AppleWebKit/537.36(KHTML,如Gecko)Chrome/38.02125.104 Safari/537.3"["last_activity"]=>int(1414848680)["user_data"]=>字符串(0)"}
user_data部分为空。
这是我的代码(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');
}
}
}
和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
求你了!帮帮我!这个错误让我疯了!请(我试图解决控制器中调用用户数据的问题,并使用$this->load->view('nav.php',$data);
将数据发送到视图,但它不起作用…)
您是在视图还是在控制器中打印用户数据?尝试在控制器中打印。我认为这与控制器中的$this不同,这可能是问题的原因。您可能需要将$new_data传递给视图。此外,代替
echo "<script>
location.href='/index.php/main';
</script>";
你可以使用
redirect(main);
重定向服务器端
您正在加载会话类吗?
$this->load->library("session");
我总是将会话设置为自动加载。但当我忘记的时候,我会遇到这种问题。
我可能错了,因为我不知道你的代码在哪里运行,但如果你是本地主机,你可能会发现浏览器拒绝你的cookie。你也有
$config['cookie_secure'] = TRUE
和'cookie_secure'=只有在存在安全HTTPS连接的情况下才会设置cookie。