简体   繁体   English

php无法存储任何_SESSION或_GLOBALS变量

[英]php fails to store any _SESSION or _GLOBALS variable

I am storing a session and a global variable in file1.php. 我在file1.php中存储一个会话和一个全局变量。 However, when I try to access those from file2.php I get nothing. 但是,当我尝试从file2.php访问那些文件时,我什么也没得到。 I am using php 5.1.6. 我正在使用PHP 5.1.6。

$_SESSION['abc'] = $a;
$GLOBALS['def'] = $b;

Any idea? 任何想法?

Thanks in advance. 提前致谢。

  • Do you have cookies enabled on your browser? 您的浏览器是否启用了cookie?
  • Are you remembering to call session_start at the top of BOTH pages? 您还记得在两个页面的顶部都调用session_start吗?
  • Are you storing your session variables in $_SESSION ? 您是否将会话变量存储在$ _SESSION中? Nothing else will store. 没有其他东西可以存储了。

In regards to your edit: The variable stored in $GLOBALS is just a global to that script. 关于您的编辑:$ GLOBALS中存储的变量只是该脚本的全局变量。 You have to put the value in $_SESSION to use across pages. 您必须将值放在$ _SESSION中才能在页面之间使用。

Example: 例:

// Page 1
session_start();
$_SESSION['abc'] = "hello world";
$GLOBALS['def']  = "More stuff.";

// Page 2
session_start();
echo $_SESSION['abc'];   // prints 'hello world'
echo $GLOBALS['def'];   // is not defined. Globals aren't session variables.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM