php 如何获取 codeigniter 会话 userdata 变量的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29325425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:21:16  来源:igfitidea点击:

How can I get the value of a codeigniter session userdata variable?

phpcodeigniterwebcodeigniter-2

提问by Prateek Joshi

I have generated the array containing session data as,

我生成了包含会话数据的数组,

array(8) {
  ["session_id"]=> string(32) "1dc5eb3730601a3447650fa4453f36cd"
  ["ip_address"]=> string(3) "::1" 
  ["user_agent"]=> string(102) "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36" 
  ["last_activity"]=> int(1427602284) 
  ["user_data"]=> string(0) "" 
  ["roll_no"]=> string(6) "111416" 
  ["is_logged_in"]=> int(1) 
  [0]=> object(stdClass)#20 (6) 
    { 
      ["name"]=> string(13) "durex" 
      ["email"]=> string(25) "[email protected]" 
      ["year"]=> string(1) "2" 
      ["semester"]=> string(1) "4" 
      ["branch"]=> string(2) "IT" 
      ["parent_email"]=> string(27) "[email protected]" 
    } 
}

How can I echo name(durex) using the above session $userdata?

如何使用上述会话 $userdata 回显名称(杜蕾斯)?

回答by Jacob

This is the process you would follow if you wanted to set, and then retrieve, some userdata set in Codeigniter 2.0

如果您想在 Codeigniter 2.0 中设置然后检索一些用户数据,您将遵循此过程

<?php
        $user_array = array(
          'name' => 'bob',
          'email' => '[email protected]'
        );

        $this->session->set_userdata('userdata', $user_array);
        
        $user_data = $this->session->userdata('userdata');

        //Returns User's name 
        echo $user_data['name'];
?>

I'm not sure if what you pasted was formatted correctly.. also please do a print_ror var_dump($user_data)to see what it is pulling in on the $user_datavariable

我不确定您粘贴的内容是否格式正确……也请执行print_rvar_dump($user_data)查看它对$user_data变量的影响

Edit: Now that you've updated your post, it is clear to me that what you need to do is this.

编辑:现在你已经更新了你的帖子,我很清楚你需要做的是这个。

<?php
$user_data = $this->session->userdata('0');

echo $user_data->name;
?>

回答by Arzgethalm

Please check if this works. If you are getting it from the session values:

请检查这是否有效。如果您从会话值中获取它:

<?php
        $user_data = $this->session->userdata('user_data');
        echo $user_data[0]['name'];
?>

if you are getting if from your array:

如果你从你的数组中得到 if:

<?php

        echo $your_array_name['user_data'][0]['name'];
?>

Note: This is the correct format of the array in PHP

注意:这是PHP中数组的正确格式

<?php
$my_array = Array ( 
  'session_id' => '8f9286115a38df9ed54a65b135d4e8c0',
  'ip_address' => '::1',
  'user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
  'last_activity' => 1427600580,
  'roll_no' => 111416, 
  'is_logged_in' => 1,
  'email' => '',
  'user_data' => Array ( 
     0 => Array ( 
       'name' => 'durex', 
       'email' => '[email protected]', 
       'year' => 2 ,
       'semester' => 4, 
       'branch' => 'CSE', 
       'parent_email' => '[email protected]'
     )
  )
);
echo $my_array['user_data'][0]['name'];
?>

回答by Bharat Kr. Lal

1.get your session and save in variable $usernya = $this->session->get_userdata('sess');

1.获取您的会话并保存在变量 $usernya = $this->session->get_userdata('sess');

2.Now you can show the data with simple echo echo $usernya['username'];

2.现在你可以用简单的 echo echo $usernya['username']; 显示数据

回答by digout

In Codeigniter 3you can retrieve session data by simply using PHP's native $_SESSIONvariable

Codeigniter 3 中,您只需使用 PHP 的本机$_SESSION变量即可检索会话数据

$_SESSION['item']

Or through the magic getter

或者通过魔法吸气剂

$this->session->item

It still allows for the traditional

它仍然允许传统的

$this->session->userdata('item');

More in the docs

文档中的更多内容