Codeigniter会话user_data为空

时间:2018-09-21 04:51:38

标签: php session codeigniter-2

我正在尝试在Codeigniter会话(数据库)中存储以下数据

$session_data = array(
    "name"=>$user_details['name'],
    "email"=>$user_details['email'],
    "role"=>$user_details['role'],
    "status"=>$user_details['status'],
    "mobile"=>$user_details['mobile'],
    "permissions"=>json_decode($user_details['permissions'],TRUE),
    "country_id" => $country_details['country_id'],
    "country_name" => $country_details['name'],
    // "countries" => $active_countries
);
$this->session->set_userdata("user",$session_data);

这绝对好,并且数据存储在数据库中,如下所示,在user_data表的ci_sessions列下。

a:2:{s:9:"user_data";s:0:"";s:4:"user";a:8:{s:4:"name";s:9:" Test";s:5:"email";s:18:"email@email.com";s:4:"role";s:11:"super_admin";s:6:"status";s:1:"1";s:6:"mobile";s:10:"1234567890";s:11:"permissions";a:9:{s:9:"dashboard";a:1:{i:0;s:3:"all";}s:8:"settings";a:1:{i:0;s:3:"all";}s:9:"employees";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}s:7:"drivers";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}s:5:"rides";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}s:5:"users";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}s:4:"cars";a:1:{i:0;s:4:"list";}s:3:"ads";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}s:7:"country";a:5:{i:0;s:4:"list";i:1;s:3:"add";i:2;s:4:"edit";i:3;s:6:"delete";i:4;s:4:"view";}}s:10:"country_id";s:3:"174";s:12:"country_name";s:5:"QATAR";}}

但是,当我取消对$session_data数组中的countrys数组索引的注释时,会在数据库中创建记录,但是user_data列值为空。但是当我在同一个控制器中打印时,它会显示所有数据

$session_data = array(
    "name"=>$user_details['name'],
    "email"=>$user_details['email'],
    "role"=>$user_details['role'],
    "status"=>$user_details['status'],
    "mobile"=>$user_details['mobile'],       
    "permissions"=>json_decode($user_details['permissions'],TRUE),
    "country_id" => $country_details['country_id'],
    "country_name" => $country_details['name'],
    "countries" => $active_countries
);
$this->session->set_userdata("user",$session_data);
print_r($this->session->userdata['user']);

Array ( [name] => Ways Test [email] => test@email.com [role] => super_admin [status] => 1 [mobile] => 1234567890 [permissions] => Array ( [dashboard] => Array ( [0] => all ) [settings] => Array ( [0] => all ) [employees] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) [drivers] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) [rides] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) [users] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) [cars] => Array ( [0] => list ) [ads] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) [country] => Array ( [0] => list [1] => add [2] => edit [3] => delete [4] => view ) ) [country_id] => 174 [country_name] => QATAR [countries] => Array ( [0] => Array ( [country_id] => 99 [iso_name] => IN [name] => INDIA [image] => uploads/country_image/IN.png [nicename] => India [iso3] => IND [numcode] => 356 [phonecode] => +91 [currency_code] => Indian rupee [currency_name] => INR [currency_symbol] => ₹ [distance_unit] => kilometer [status] => 1 ) [1] => Array ( [country_id] => 174 [iso_name] => QA [name] => QATAR [image] => uploads/country_image/QA.png [nicename] => Qatar [iso3] => QAT [numcode] => 634 [phonecode] => +974 [currency_code] => Qatari riyal [currency_name] => QR [currency_symbol] => ر.ق [distance_unit] => kilometer [status] => 1 ) ) )

假设这可能是内存大小问题,我将user_data的mysql数据类型从Text更改为LargeText。还是行不通。

Cookie有问题吗?我的假设是,cookie只是保存会话数据库的引用,而不是实际数据。因为,我觉得Cookie的大小不是问题

配置:

$config["sess_cookie_name"] = "ci_sessions";
$config["sess_expiration"] = 7200;
$config["sess_expire_on_close"] = FALSE;
$config["sess_encrypt_cookie"] = FALSE;
$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;

$config["cookie_prefix"] = "";
$config["cookie_domain"] = "";
$config["cookie_path"] = "/";
$config["cookie_secure"] = FALSE;

数据库结构:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
  `session_id` varchar(40) NOT NULL DEFAULT '0',
  `ip_address` varchar(45) NOT NULL DEFAULT '0',
  `user_agent` varchar(120) NOT NULL,
  `last_activity` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `user_data` longtext NOT NULL,
  PRIMARY KEY (`session_id`),
  KEY `last_activity_idx` (`last_activity`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Authentication.php(控制器)

if($result['success']===true && $result['user_details']['status']=='1'){
    $user_details = $result['user_details'];
    $model_call = $this->country_model->active_countries();
    $active_countries = $model_call['active_countries'];
    // Getting country details
    $model_call = $this->country_model->get_country_details($user_details['country']);
    $country_details = $model_call['country_details'];

    $session_data = array(
        "name"=>$user_details['name'],
        "email"=>$user_details['email'],
        "role"=>$user_details['role'],
        "status"=>$user_details['status'],
        "mobile"=>$user_details['mobile'],
        "permissions"=>json_decode($user_details['permissions'],TRUE),
        "country_id" => $country_details['country_id'],
        "country_name" => $country_details['name'],
        "countries" => $active_countries
    );
    $this->session->set_userdata("user",$session_data);
    redirect("myadmin/dashboard");
}

Dashboard.php(控制器)

public function __construct(){
    parent::__construct();
    var_dump($this->session->all_userdata());
    //var_dump($this->session->userdata['user']);
    exit();
    if(!isset($this->session->userdata['user'])){
        redirect("myadmin/authentication"); 
    }
}

我使用的是Codeigniter 2.2.4版本

2 个答案:

答案 0 :(得分:0)

但是,发布表结构将非常有帮助,这可能是因为国家/地区索引本身。我假设您正在使用$ this-> db-> insert将此会话保存到数据库中。国家/地区索引是一个数组,因此会产生服务器错误。我建议您调试用于插入这些数据的模型。

在插入后输入以下代码应显示错误。

$ this-> db-> _ error_message(); $ this-> db-> _ error_number();

答案 1 :(得分:0)

尝试更新到新版本的codeigniter,我遇到了类似的问题,并且可以解决!