php 如何打印当前设置的所有会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3331613/
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
How to print all session variables currently set?
提问by Georgy
Without having to call each session variable by name, is there a way to display the content of all the session variables currently set?
不必按名称调用每个会话变量,有没有办法显示当前设置的所有会话变量的内容?
回答by alex
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
Or you can use print_rif you don't care about types. If you use print_r, you can make the second argument TRUEso it will return instead of echo, useful for...
或者,print_r如果您不关心类型,则可以使用。如果使用print_r,则可以设置第二个参数,TRUE以便它返回而不是 echo,这对...
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
回答by Linda Cox
<?php
session_start();
echo "<h3> PHP List All Session Variables</h3>";
foreach ($_SESSION as $key=>$val)
echo $key." ".$val."<br/>";
?>
回答by Artefacto
Not a simple way, no.
不是一个简单的方法,不。
Let's say that by "active" you mean "hasn't passed the maximum lifetime" and hasn't been explicitly destroyed and that you're using the default session handler.
假设“活动”是指“还没有超过最大生命周期”并且没有被明确销毁,并且您正在使用默认会话处理程序。
- First, the maximum lifetime is defined as a php.ini config and is defined in terms of the last activity on the session. So the "expiry" mechanism would have to read the content of the sessions to determine the application-defined expiry.
- Second, you'd have to manually read the sessions directory and read the files, whose format I don't even know they're in.
- 首先,最大生命周期定义为 php.ini 配置,并根据会话中的最后一个活动定义。因此,“到期”机制必须读取会话的内容以确定应用程序定义的到期时间。
- 其次,您必须手动读取会话目录并读取文件,我什至不知道它们的格式。
If you really need this, you must implement some sort of custom session handler. See session_set_save_handler.
如果你真的需要这个,你必须实现某种自定义会话处理程序。见session_set_save_handler。
Take also in consideration that you'll have no feedback if the user just closes the browser or moves away from your site without explciitly logging out. Depending on much inactivity you consider the threshold to deem a session "inactive", the number of false positives you'll get may be very high.
还要考虑到,如果用户只是关闭浏览器或离开您的站点而没有明确退出,您将不会收到任何反馈。根据您认为会话“不活跃”的阈值,您将获得的误报数量可能非常高,这取决于您的不活动程度。
回答by Young
You could use the following code.
您可以使用以下代码。
print_r($_SESSION);
回答by Mosin
this worked for me:-
这对我有用:-
<?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>
<?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>
thanks for sharing code...
感谢分享代码...
Array
(
[__ci_last_regenerate] => 1490879962
[user_id] => 3
[designation_name] => Admin
[region_name] => admin
[territory_name] => admin
[designation_id] => 2
[region_id] => 1
[territory_id] => 1
[employee_user_id] => mosin11
)
回答by Atul Baldaniya
session_start();
echo '<pre>';var_dump($_SESSION);echo '</pre>';
// or
echo '<pre>';print_r($_SESSION);echo '</pre>';
NOTE: session_start(); line is mustthen only you will able to print the value $_SESSION
注意: session_start(); line is must那么只有你才能打印值 $_SESSION
回答by CrazyTim
Echo the session object as json. I like json because I have a browser extension that nicely formats json.
将会话对象回显为 json。我喜欢 json,因为我有一个浏览器扩展,可以很好地格式化 json。
session_start();
echo json_encode($_SESSION);

