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

我正在使用 Codeigniter 3 开发社交网络应用程序,Ion-AuthGithub 存储库 和 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)。


缺什么?


蓝山帝景
浏览 126回答 1
1回答

MM们

快速检查$user中的内容Ion_auth_model.php、set_session() 正在使用 var_dump 显示它是...LINE: 1944 Module Ion_auth_modelobject(stdClass)#26 (5) {&nbsp; ["email"]=>&nbsp; string(21) "frednurk@gmail.com"&nbsp; ["id"]=>&nbsp; string(1) "8"&nbsp; ["password"]=>&nbsp; string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"&nbsp; ["active"]=>&nbsp; string(1) "1"&nbsp; ["last_login"]=>&nbsp; string(10) "1599322333"}不包含first_name。由于错误消息,这正是您所期望的。搜索调用此函数的位置,将带您返回 Ion_auth_model.php - 登录,其中 $user 的选择查询是$query = $this->db->select($this->identity_column . ', email, id, password, active, last_login')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where($this->identity_column, $identity)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->limit(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->order_by('id', 'desc')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->get($this->tables['users']);正如预期的那样,它丢失了first_name。因此,将first_name 添加到选择中,如下所示...$query = $this->db->select($this->identity_column . ', email, id, password, active, last_login, first_name')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where($this->identity_column, $identity)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->limit(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->order_by('id', 'desc')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->get($this->tables['users']);结果 $user 变成...LINE: 1944 Module Ion_auth_modelobject(stdClass)#26 (6) {&nbsp; ["email"]=>&nbsp; string(21) "frednurk@gmail.com"&nbsp; ["id"]=>&nbsp; string(1) "8"&nbsp; ["password"]=>&nbsp; string(60) "$2y$10$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"&nbsp; ["active"]=>&nbsp; string(1) "1"&nbsp; ["last_login"]=>&nbsp; string(10) "1599322333"&nbsp; ["first_name"]=>&nbsp; string(3) "Tim"}然后就一切皆大欢喜了。结论阅读错误消息通过检查变量/数组/对象来查看它们是什么来执行调试。回来修复它。调试时,我使用了这个小代码片段echo '<pre>';echo 'LINE: '. __LINE__. ' Module '.__CLASS__.'<br>';var_dump(WHAT_TO_LOOK_AT_GOES_HERE);echo '</pre>';
打开App,查看更多内容
随时随地看视频慕课网APP