Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $GLOBALS missing $_SERVER

According to http://www.php.net/manual/en/reserved.variables.globals.php :

An associative array containing references to all variables which are currently defined in the global scope of the script.

So, following code must display that $GLOBALS var has _SERVER, _ENV (if it is enabled in variables_order in php.ini) and _REQUEST keys:

var_dump($GLOBALS); 

The result is:

  • Under nginx + php-fpm: missing _SERVER, _ENV, _REQUEST
  • Under cli: missing _ENV, _REQUEST

Hmm.. perhaps there is smth in docs about this behavior? I've looked through every page for each variable:

  • _SERVER: http://www.php.net/manual/en/reserved.variables.server.php
  • _ENV: http://www.php.net/manual/en/reserved.variables.request.php
  • _REQUEST: http://www.php.net/manual/en/reserved.variables.request.php

And i have found no mentions about such behaviour. Why it works like that?

I have installed php using debian package from http://www.dotdeb.org/ repo (nothing was compiled manually)... Currently running with nginx + php5-fpm. Is that a php bug?

like image 570
avasin Avatar asked Jul 09 '13 10:07

avasin


People also ask

What does $globals mean in PHP?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

What is $_ SERVER in PHP?

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

What is $_ SERVER [' Script_name ']?

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

What is $_ SERVER [' Request_uri ']?

$_SERVER['REQUEST_URI'] contains the URI of the current page. So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials. php.


1 Answers

I've created a bug on php.net website, and php team answered: https://bugs.php.net/bug.php?id=65223

Summary:

This is not a bug. super-globals (aka. auto globals) are not added to symbol tables by default for performance reasons unless the parser sees need. i.e.

<?php $_SERVER; print_r($GLOBALS); ?>

will list it. You can also control this using auto_globals_jit in php.ini: http://www.php.net/manual/en/ini.core.php#ini.auto-globals-jit

Thanks php team so answer so fast!

like image 172
avasin Avatar answered Sep 24 '22 02:09

avasin