PHP,print_r($_SESSION) 不显示其内容?

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

PHP, print_r($_SESSION) doesn't show its content?

phpsession

提问by aneuryzm

I want to see the content of the the array $_SESSION with the command print_r($_SESSION) but what I get is just the following output:

我想用命令 print_r($_SESSION) 查看数组 $_SESSION 的内容,但我得到的只是以下输出:

Array ()

what am I missing ?

我错过了什么?

thanks

谢谢

回答by Lizard

Make sure you call session_start()at the top of all the pages you wish to use the session.

确保session_start()在您希望使用会话的所有页面的顶部调用。

http://php.net/manual/en/function.session-start.php

http://php.net/manual/en/function.session-start.php

<?php

session_start();

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

?>

回答by ma?ek

Note<?php session_start(); ?>must be called before any other output is sent to the browser.

Note<?php session_start(); ?>必须在任何其他输出发送到浏览器之前调用。

index.php

索引.php

<?php

session_start();
$_SESSION['hello'] = 'world';
print_r($_SESSION);

?>

Output

输出

Array (
  [hello] => world
)

回答by Luis Melgratti

Most likely you are missing.

很可能你失踪了。

session_start();

回答by Hat programmers

Actually Its Printing The Session Variables But You Have Not Set Any Before Therefore The Array Returned By print_r($_SESSION) is Empty Try Setting The Variables First and Then Print Them.

实际上它打印会话变量,但您之前没有设置任何因此print_r($_SESSION)返回的数组为空尝试先设置变量然后打印它们。

Remember that session_start(); should always be first line.

记住 session_start(); 应该总是第一行。