Codeigniter 3应用程序错误:无法在会话中存储某些列值

问题描述

我正在使用Codeigniter 3, Ion-Auth 和Bootstrap 4开发社交网络应用程序。您可以看到 Github repo HERE

我想将登录用户的 first_name 存储到当前会话中。为此,我在原始$session_data数组中添加了行'user_first_name' => $user->first_name,

$session_data = [
        'identity'                 => $user->{$this->identity_column},$this->identity_column     => $user->{$this->identity_column},'email'                    => $user->email,'user_id'                  => $user->id,'user_first_name'          => $user->first_name,//retrieve from the first_name column
        'old_last_login'           => $user->last_login,'last_check'               => time(),'ion_auth_session_hash'    => $this->config->item('session_hash','ion_auth'),];

关键是在用户ID登录后显示欢迎消息:

Welcome,<?php echo $this->session->userdata('user_first_name'); ?>

我收到Undefined property $first_name消息,欢迎消息中只有“ Welcome”(尽管$this->session->userdata('user_first_name') 确实返回了用户ID)。

>

缺少什么?

解决方法

快速检查 Ion_auth_model.php set_session()中的$user使用的是var_dump就是...

LINE: 1944 Module Ion_auth_model
object(stdClass)#26 (5) {
  ["email"]=>
  string(21) "frednurk@gmail.com"
  ["id"]=>
  string(1) "8"
  ["password"]=>
  string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
  ["active"]=>
  string(1) "1"
  ["last_login"]=>
  string(10) "1599322333"
}

其中不包含名字。由于出现错误消息,您期待的是什么。

搜索此位置将使您返回Ion_auth_model.php-登录,其中$ user的选择查询为

$query = $this->db->select($this->identity_column . ',email,id,password,active,last_login')
                  ->where($this->identity_column,$identity)
                  ->limit(1)
                  ->order_by('id','desc')
                  ->get($this->tables['users']);

和预期的一样,缺少 first_name

因此将first_name添加到Select中,就像这样...

$query = $this->db->select($this->identity_column . ',last_login,first_name')
                  ->where($this->identity_column,'desc')
                  ->get($this->tables['users']);

导致$ user成为...

LINE: 1944 Module Ion_auth_model
object(stdClass)#26 (6) {
  ["email"]=>
  string(21) "frednurk@gmail.com"
  ["id"]=>
  string(1) "8"
  ["password"]=>
  string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
  ["active"]=>
  string(1) "1"
  ["last_login"]=>
  string(10) "1599322333"
  ["first_name"]=>
  string(3) "Tim"
}

然后就很开心。

结论

  1. 阅读错误消息
  2. 通过检查变量/数组/对象以查看其内容来执行调试。
  3. 重新进行修复。

调试时,我有这个小的代码段

echo '<pre>';
echo 'LINE: '. __LINE__. ' Module '.__CLASS__.'<br>';
var_dump(WHAT_TO_LOOK_AT_GOES_HERE);
echo '</pre>';

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...