简体   繁体   English

PHP应用程序全局变量

[英]PHP application global variable

I want to store a global variable that exist as long as the application is running and accessible from all the sessions. 我想存储一个存在的全局变量,只要该应用程序正在运行并且可以从所有会话中访问。 I tried $GLOBALS but it's not durable. 我尝试了$ GLOBALS,但它不耐用。 It's temporary, I store the data and when I request the same code again, it says that this index not recognised. 这是临时的,我存储了数据,当我再次请求相同的代码时,它说该索引未被识别。

Will I have to store my value in a file? 我需要将我的值存储在文件中吗?

Edit :: this is a sample code that always print 'not set' 编辑::这是始终打印“未设置”的示例代码

if(isset($GLOBALS['myindex']){
   echo 'set';
   echo $GLOBALS['myindex'];
}else{
   echo 'not set';
   $GLOBALS['myindex']='myValue';
}

Best would be to go with Sessions: 最好是使用Sessions:

Make sure you add session_start(); 确保添加session_start(); at the beginning of the application and it stays throughout the pages. 在应用程序的开始处,并停留在整个页面中。 Now you can assign the value to it. 现在,您可以为其分配值。 via : 通过:

 $_SESSION['yourkey'] = 'yourvalue'; // you can access it throughout the application

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

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