php Codeigniter - 获取所有用户会话数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22149751/
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-25 04:41:44  来源:igfitidea点击:

Codeigniter - get all users session data

phpsessioncodeigniter-3session-variables

提问by Sabri Aziri

I need to get all sessions data and take some actions upon them, is there any possible way to get them, I found how to solve it in php but codeigniter use's it own custom sessions library.

我需要获取所有会话数据并对它们采取一些操作,有没有可能的方法来获取它们,我找到了如何在 php 中解决它,但是 codeigniter 使用它自己的自定义会话库。

Native php

原生 php

$_SESSION

$_SESSION

How can I do it in codeigniter

我怎样才能在 codeigniter 中做到这一点

回答by Mohan

To get all session data you can use $this->session->all_userdata();

要获取您可以使用的所有会话数据 $this->session->all_userdata();

To print all your session variable use this line anywhere in your code :

要打印所有会话变量,请在代码中的任何位置使用此行:

echo '<pre>'; print_r($this->session->all_userdata());exit;

回答by stavgian

You can use db session. So data will be stored to the database. See Saving Session Data to a Database

您可以使用数据库会话。因此数据将存储到数据库中。请参阅将会话数据保存到数据库

If you have no database support just use cookie and get the data with

如果您没有数据库支持,只需使用 cookie 并获取数据

$username = 'John Doe';
$this->session->set_userdata('username',$username);
$var = $this->session->userdata;
echo $var['username'];

回答by Manish

To get all users session data.First we need to initialize the Session class manually in our controller constructor use following code.

要获取所有用户会话数据。首先,我们需要使用以下代码在控制器构造函数中手动初始化 Session 类。

$this->load->library('session');

Then we use following code.

然后我们使用下面的代码。

$this->session->all_userdata();

Above function return an associative array like the following.

上面的函数返回一个关联数组,如下所示。

Array
( [session_id] => 4a5b5dca45708ab1a84364eeb455j007 [ip_address] => 127.0.0.1 [user_agent] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; [last_activity] => 1102132923
)

For complete Ci Session tutorial. Please referrer following link

完整的 Ci Session 教程。请参考以下链接

回答by Marius Cucuruz

If you want to see everythingthe way I've done it is to cast $this->sessionas array then print_rit.

如果您想按照我所做的方式查看所有内容,则将其转换$this->session为数组print_r

        $this->load->library('session');
        echo "<pre>". print_r((array)$this->session, true) ."</pre>";

Hope this helps.

希望这可以帮助。

回答by AlfhaOmega

$this->session->userdata('data')->username

回答by pgee70

If you know how to do this in PHP why not implement that, and not use the codeigniter session class? my experience is that the CI session class breaks pretty badly with ajax calls and proxy servers, so it is best avoided.

如果您知道如何在 PHP 中执行此操作,为什么不实现它,而不使用 codeigniter 会话类?我的经验是 CI 会话类在使用 ajax 调用和代理服务器时非常严重,因此最好避免使用。