Questions tagged [superglobals]

Superglobals are built-in variables that are always available in all scopes.

Superglobals are built-in variables that are always available in all scopes.

Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

These superglobal variables are:

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV

https://php.net/manual/en/language.variables.superglobals.php

235 questions
6
votes
2 answers

PHP: INPUT_POST (used in filter_input_array) overwrites all previous modifications of $_POST

the INPUT_POST Parameter of the PHP filter function filter_input_array() e.g. in filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); seems to overwrite any modification applied to the superglobal $_POST. test:
Robert TheSim
  • 433
  • 5
  • 6
5
votes
4 answers

Is $_SERVER['SERVER_ADDR'] always set?

Is $_SERVER['SERVER_ADDR'] always set? Should I check with isset() or is that unnecessary? I need to get the IP of the site so I can find out if it's 127.0.0.1/localhost
Nick
  • 53
  • 1
  • 3
5
votes
2 answers

At what point in the startup process does PHP set the REQUEST_TIME variables

As noted in the PHP documentation, the $_SERVER superglobal array contains two elements, REQUEST_TIME and REQUEST_TIME_FLOAT, both of which contain the timestamp of the start of the request, in varying precision levels. I am currently using the…
akukas
  • 555
  • 1
  • 6
  • 11
5
votes
4 answers

Is it possible to trigger an error when $_POST or another superglobal is accessed?

I have this framework project kind of on the backburner where I want to enforce the use of an Input class to access all superglobals like $_POST, $_GET and $_SERVER. A recent question here reminded me about it. The class will do a little cleanup of…
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
4
votes
4 answers

Do you consider it bad form in PHP to access super globals within class methods?

Take an example login() function within a class Account. class Account { /* Class variables */ public function login() { if(isset($_POST['username']) && isset($_POST['password'])) return $this->_formLogin(); else…
Frank Crook
  • 1,466
  • 12
  • 19
4
votes
3 answers

Safe to Overwrite super-global $_SESSION?

Is it safe to overwrite the super-global $_SESSION with a specialised session object? class SessionObject implements ArrayAccess { ... } ... // Session data has just been deserialised from store. $_SESSION = new SessionObject( $session_data…
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
4
votes
2 answers

php interpreter and superglobals

first of all, I'm a French student, so excuse me for my poor English level. We are currently developing a web server (C++) and I must develop the CGI execution part, more exactly : The PHP CGI part. When a user ask a .php page on our server, we…
user702041
  • 53
  • 5
4
votes
2 answers

$GLOBALS superglobal gets modified when passed to a function

I came across some weird behavior in PHP: function f($var) { // not using references foreach ($var as $k => $v) { unset($var[$k]); // shouldn't this unset from a copy?! } } print '
';
var_dump($GLOBALS); //…
nice ass
  • 16,471
  • 7
  • 50
  • 89
4
votes
2 answers

Is there a good alternative to $_SERVER['SERVER_NAME']?

I read the following comment on PHP doc pages: "Be warned that most contents of the Server-Array (even $_SERVER['SERVER_NAME']) are provided by the client and can be manipulated. They can also be used for injections and thus MUST be checked…
Ramon K.
  • 3,402
  • 3
  • 20
  • 29
3
votes
2 answers

PHP Array key strings without quotation marks

I am moving the files to the server and is using variables like $_GET[mode] without ''(single quotes) in 'mode'. It works perfectly locally but on the server i am getting notices.. How can i overcome this?here is my phpinfo file phpinfo Is there…
June
  • 793
  • 2
  • 17
  • 43
3
votes
3 answers

Is the order of key-value pairs guaranteed in the superglobal $_GET?

Is the order of the keys-value pairs in the $_GET superglobal variable guaranteed to be consistent with how the field-value pairs were received in the requested URL? For example, given this URL request received by the web…
Robert Groves
  • 7,574
  • 6
  • 38
  • 50
3
votes
2 answers

Should I store superglobals as the wrapper class' property or should I access it directly?

I wanted to create a wrapper for Session and Request so that I don't have to access the PHP superglobals directly. I realized that if I create a wrapper for the superglobals and use them, unit testing my application will be easier as the wrapper…
rickchristie
  • 1,640
  • 1
  • 17
  • 29
3
votes
4 answers

Error using $_SERVER in a variable variable (PHP)

I was trying to get the name of a superglobal variable through a GET parameter. I was told to pass only _VAR_NAME (without the $) in the get request, so in the program I have to access it through a variable variable:…
David Casillas
  • 1,801
  • 1
  • 29
  • 57
3
votes
2 answers

Why PHP needs to store the same global data recursively?

Here is a result of a random var_dump($GLOBALS): array(6) { ["_GET"] => array(0) {} ["_POST"] => array(0) {} ["_COOKIE"]=> array(1) { ["PHPSESSID"]=> string(26) "o8f2mggog45mq9p5ueafgu5hv6" } ["_FILES"] => array(0) {} …
wpclevel
  • 2,451
  • 3
  • 14
  • 33
3
votes
1 answer

I upgraded to php 7 and the superglobal request is empty

I updated from PHP 5.6 to PHP 7. every $_REQUEST['some_var'] I do returns an error that it is not set... is it normal that $_REQUEST is empty ? Example : echo $_REQUEST['login_ID'] returns Notice: Undefined index: login_ID in
user2986055
1
2
3
15 16