Own SuperGlobal Variable in PHP?

2019-09-04 16:05发布

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...

5条回答
Luminary・发光体
2楼-- · 2019-09-04 16:19

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

查看更多
成全新的幸福
3楼-- · 2019-09-04 16:24

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楼-- · 2019-09-04 16:25

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.

查看更多
做自己的国王
5楼-- · 2019-09-04 16:32

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

查看更多
别忘想泡老子
6楼-- · 2019-09-04 16:34

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.

查看更多
登录 后发表回答