简体   繁体   English

更改视图PHP / CodeIgniter时会话丢失

[英]Session is lost on change of the view PHP / CodeIgniter

I´m developing an app, and I´m using session variables to identify the logged user, all works fine until I change the view from the index. 我正在开发一个应用程序,并且正在使用会话变量来标识登录的用户,在我从索引更改视图之前,所有方法都可以正常工作。 This is my login controller 这是我的登录控制器

$this->session->set_userdata('id', $obj[0]->ID);
header('Location: mydomain.com/landing');

I have not problem with this, actually, if I do 实际上,我对此没有问题

echo $this->session->userdata('id');

It returns the previous information stored on the session. 它返回存储在会话上的先前信息。

I loaded the session librery on autoload.php file 我在autoload.php文件中加载了会话librery

$autoload['libraries'] = array('database', 'session');

When I try to load the landing view, exists a validation to know who is logged 当我尝试加载着陆视图时,存在一个验证以了解记录了谁

<?php defined('BASEPATH') OR exit('No direct script access allowed');
  if( !$this->session->has_userdata('id') ){
    header('Location: midominio.com');
  }
  else{
?>
<html></html>
<?php
}
?>

Every time it redirects me to the index, if I print the $this->session it returns 每次它将我重定向到索引时,如果我打印$this->session它将返回

object(CI_Session)#16 (4) {
  ["userdata"]=>
  &array(1) {
    ["__ci_last_regenerate"]=>
    int(1537469720)
  }
  ["_driver":protected]=>
  string(5) "files"
  ["_config":protected]=>
  &array(9) {
    ["cookie_lifetime"]=>
    int(7200)
    ["cookie_name"]=>
    string(10) "ci_session"
    ["cookie_path"]=>
    string(1) "/"
    ["cookie_domain"]=>
    string(29) "mydomain.com"
    ["cookie_secure"]=>
    bool(false)
    ["expiration"]=>
    int(7200)
    ["match_ip"]=>
    bool(false)
    ["save_path"]=>
    string(34) "/opt/alt/php70/var/lib/php/session"
    ["_sid_regexp"]=>
    string(12) "[0-9a-v]{32}"
  }
  ["_sid_regexp":protected]=>
  string(12) "[0-9a-v]{32}"
}

Id from the login is not set. 未设置登录ID。 What is wrong? 怎么了? I´m using PHP7 and CI 3x 我正在使用PHP7和CI 3x

Please set below variables in config files. 请在配置文件中设置以下变量。

$config['sess_cookie_name'] = 'ci_session';
$config['sess_save_path'] = 'ci_sessions';

If ci_sessions table is not exists then please create below table : 如果ci_sessions表不存在,请在下面的表中创建:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

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

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