Question

As we know that session variable $_SESSION is different for each user. I want to know the scope of Server variable like $_SERVER. I am doing http authentication in my RestFul API. If I set the $_SERVER['PHP_AUTH_USER'], will that be set for 1 user or all the user who access my server page?

Thanks

Was it helpful?

Solution

That will be set only for current user, but why don't you use $GLOBALS instead of $_SERVER?

$GLOBALS — References all variables available in global scope

This superglobal array would be more appropriate than $_SERVER for such a task. Mind, that $GLOBALS or $_SERVER don't save its data after request is finished. So if you want to keep some data from one request to another you should use $_SESSION.

OTHER TIPS

php.net:

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

that mean is different between user because session id is different per user and Session allow user access to owner associative array

the value that is set for $_SERVER is on RAM and didn't store in file or database and it remove from RAM after request finish. for this kind of work like save a variable during of user is online you can use $_SESSION but if you want save variable for all user you can use Database

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top