Own SuperGlobal Variable in PHP?

痴心易碎 提交于 2019-12-13 20:32:19

问题


I was reading something about SuplerGlobals like $_SERVER or (see more detail PHP Manual Superglobals) the other day, now, I'm asking me:

Is it possible to implement own SuperGlobals? Beside of Constants...

So for example user A writes something in the Variable which, if User B is calling it can see. Something like a server wide Session Variable or something.

Please don't be to hard, if its a silly question :) I know there are couple of ways outside, like SQL, Xml and Stuff, but maybe...


回答1:


PHP doesn't have context which can be shared between users. You should some replacement like SQL server or file. You may also check some extensions like memcache which might help you achieve your goal.




回答2:


Your whole idea of PHP superglobals it wrong.
These variables are always available in terms of just one script runtime, no the whole site.




回答3:


I was reading something about SuplerGlobals like $_SERVER or (see more detail PHP Manual Superglobals) the other day, now, I'm asking me:

Is it possible to implement own SuperGlobals? Beside of Constants...

Yes it is possible if you've got the PHP runkit extension.

So for example user A writes something in the Variable which, if User B is calling it can see

That's not what superglobals do - they are variables which exist in global scope (i.e. for the duration of an instance of a script).

If you want to share data between different invocations then you need to send it to your storage tier or (in the case of data for a single client) out to the browser.

Since what you are describing here is effectively a shared session, then the sensible place to implement this would be in the session handler.




回答4:


This is not possible, you can only see your own session data.

To achieve this you would need to store the data somewhere else. in text files or in a MySQL database would be the most common.




回答5:


i suppose you can use (asterix)export yourvar="something"(asterix) and to receive it using getenv

sry, dont know how to embed asterix=`, but it is better to avoid it...

If you use apache following could be used: http://php.net/manual/en/function.apache-setenv.php

same idea, enveroinment variable



来源:https://stackoverflow.com/questions/8256675/own-superglobal-variable-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!