为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
蓝山帝景 2023-12-15 15:04:52
我正在使用 Codeigniter 3 开发社交网络应用程序,Ion-Auth。Github 存储库 和 Bootstrap 4。您可以在此处查看我想将登录用户的 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 消息,欢迎消息仅包含“欢迎,” (尽管事实上$this->session->userdata('user_first_name') 确实返回用户的ID)。缺什么?
查看完整描述

1 回答

?
MM们

TA贡献1886条经验 获得超2个赞

快速检查$user中的内容Ion_auth_model.php、set_session() 正在使用 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"

}

不包含first_name。由于错误消息,这正是您所期望的。


搜索调用此函数的位置,将带您返回 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 添加到选择中,如下所示...


$query = $this->db->select($this->identity_column . ', email, id, password, active, last_login, first_name')

                  ->where($this->identity_column, $identity)

                  ->limit(1)

                  ->order_by('id', '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>';


查看完整回答
反对 回复 2023-12-15
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信