php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69081 $_SERVER variable sometimes undefined
Submitted: 2015-02-19 14:31 UTC Modified: 2017-07-12 15:20 UTC
Votes:15
Avg. Score:4.4 ± 0.9
Reproduced:15 of 15 (100.0%)
Same Version:3 (20.0%)
Same OS:5 (33.3%)
From: djbrainnrg at gmail dot com Assigned: ab (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: master-Git-2015-02-19 (snap) OS: Windows 7 64bit
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: djbrainnrg at gmail dot com
New email:
PHP Version: OS:

 

 [2015-02-19 14:31 UTC] djbrainnrg at gmail dot com
Description:
------------
Coming from this 64bit build -> http://windows.php.net/downloads/snaps/master/r400e65e/php-master-ts-windows-vc11-x64-r400e65e.zip

Running with apache 2.4.12 Win64.
For an unknown reason sometimes, very rare, the variable $_SERVER isn't available -> I have the failure everytime at the same place but i cannot provide a reproducable example because that only happens in combination with our used framework.

Also it only happens on the third request in a row.
Request 1: Main Page Load - Normal GET request
Request 2: Ajax Request to another page - Loaded directly on pageload
Request 3: Ajax Request to another page - Loaded directly on pageload => Error happens here

I can prevent the failure by adding just the line
$_SERVER;
at the begin of the script execution chain, lets say on the entry page.

Also i can prevent this failure with this option disabled in
auto_globals_jit = Off
in php.ini

Full debug_backtrace bellow, i know it may not help that mutch but i don't have anything better for debugging right now.



Test script:
---------------
Cannot provide an example yet.

Actual result:
--------------
Notice:  Undefined variable: _SERVER in C:\www\ouced\live\modules\Ouced\src\class\Request.class.php on line 198
array(11) {
  [0]=>
  array(7) {
    ["file"]=>
    string(42) "C:\www\ouced\live\modules\Caritas\init.php"
    ["line"]=>
    int(66)
    ["function"]=>
    string(7) "getHost"
    ["class"]=>
    string(13) "Ouced_Request"
    ["object"]=>
    object(Ouced_Request)#17 (1) {
      ["headers":"Ouced_Request":private]=>
      NULL
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(59) "C:\www\ouced\live\modules\Ouced\src\class\FS\File.class.php"
    ["line"]=>
    int(408)
    ["args"]=>
    array(1) {
      [0]=>
      string(42) "C:\www\ouced\live\modules\Caritas\init.php"
    }
    ["function"]=>
    string(12) "include_once"
  }
  [2]=>
  array(7) {
    ["file"]=>
    string(58) "C:\www\ouced\live\modules\Ouced\src\class\Module.class.php"
    ["line"]=>
    int(65)
    ["function"]=>
    string(11) "includeFile"
    ["class"]=>
    string(13) "Ouced_FS_File"
    ["object"]=>
    object(Ouced_FS_File)#15 (11) {
      ["directory":"Ouced_FS_File":private]=>
      NULL
      ["filename":"Ouced_FS_File":private]=>
      NULL
      ["filenameHash":"Ouced_FS_File":private]=>
      NULL
      ["extension":"Ouced_FS_File":private]=>
      NULL
      ["filesize":"Ouced_FS_File":private]=>
      NULL
      ["accessTime":"Ouced_FS_File":private]=>
      NULL
      ["modificationTime":"Ouced_FS_File":private]=>
      NULL
      ["contents":"Ouced_FS_File":private]=>
      NULL
      ["alias":"Ouced_FS_File":private]=>
      NULL
      ["absolutePath"]=>
      string(42) "C:\www\ouced\live\modules\Caritas\init.php"
      ["relativePath"]=>
      string(24) "modules\Caritas\init.php"
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(2) {
      [0]=>
      bool(true)
      [1]=>
      bool(false)
    }
  }
  [3]=>
  array(6) {
    ["file"]=>
    string(63) "C:\www\ouced\live\modules\Ouced\src\class\Initializer.class.php"
    ["line"]=>
    int(77)
    ["function"]=>
    string(14) "registerModule"
    ["class"]=>
    string(12) "Ouced_Module"
    ["type"]=>
    string(2) "::"
    ["args"]=>
    array(1) {
      [0]=>
      string(7) "Caritas"
    }
  }
  [4]=>
  array(6) {
    ["file"]=>
    string(53) "C:\www\ouced\live\modules\Ruh\config\basic.config.php"
    ["line"]=>
    int(36)
    ["function"]=>
    string(14) "registerModule"
    ["class"]=>
    string(17) "Ouced_Initializer"
    ["type"]=>
    string(2) "::"
    ["args"]=>
    array(1) {
      [0]=>
      string(7) "Caritas"
    }
  }
  [5]=>
  array(4) {
    ["file"]=>
    string(59) "C:\www\ouced\live\modules\Ouced\src\class\FS\File.class.php"
    ["line"]=>
    int(408)
    ["args"]=>
    array(1) {
      [0]=>
      string(53) "C:\www\ouced\live\modules\Ruh\config\basic.config.php"
    }
    ["function"]=>
    string(12) "include_once"
  }
  [6]=>
  array(7) {
    ["file"]=>
    string(58) "C:\www\ouced\live\modules\Ouced\src\class\Module.class.php"
    ["line"]=>
    int(60)
    ["function"]=>
    string(11) "includeFile"
    ["class"]=>
    string(13) "Ouced_FS_File"
    ["object"]=>
    object(Ouced_FS_File)#6 (11) {
      ["directory":"Ouced_FS_File":private]=>
      NULL
      ["filename":"Ouced_FS_File":private]=>
      NULL
      ["filenameHash":"Ouced_FS_File":private]=>
      NULL
      ["extension":"Ouced_FS_File":private]=>
      NULL
      ["filesize":"Ouced_FS_File":private]=>
      NULL
      ["accessTime":"Ouced_FS_File":private]=>
      NULL
      ["modificationTime":"Ouced_FS_File":private]=>
      NULL
      ["contents":"Ouced_FS_File":private]=>
      NULL
      ["alias":"Ouced_FS_File":private]=>
      NULL
      ["absolutePath"]=>
      string(53) "C:\www\ouced\live\modules\Ruh\config\basic.config.php"
      ["relativePath"]=>
      string(35) "modules\Ruh\config\basic.config.php"
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [7]=>
  array(6) {
    ["file"]=>
    string(57) "C:\www\ouced\live\modules\Ouced\src\class\Ouced.class.php"
    ["line"]=>
    int(164)
    ["function"]=>
    string(14) "registerModule"
    ["class"]=>
    string(12) "Ouced_Module"
    ["type"]=>
    string(2) "::"
    ["args"]=>
    array(2) {
      [0]=>
      string(3) "Ruh"
      [1]=>
      bool(true)
    }
  }
  [8]=>
  array(7) {
    ["file"]=>
    string(57) "C:\www\ouced\live\modules\Ouced\src\class\Ouced.class.php"
    ["line"]=>
    int(223)
    ["function"]=>
    string(8) "initCore"
    ["class"]=>
    string(5) "Ouced"
    ["object"]=>
    object(Ouced)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [9]=>
  array(7) {
    ["file"]=>
    string(40) "C:\www\ouced\live\modules\Ouced\init.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(16) "startApplication"
    ["class"]=>
    string(5) "Ouced"
    ["object"]=>
    object(Ouced)#1 (0) {
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [10]=>
  array(4) {
    ["file"]=>
    string(46) "C:\www\ouced\live\modules\Ruh\public\index.php"
    ["line"]=>
    int(8)
    ["args"]=>
    array(1) {
      [0]=>
      string(40) "C:\www\ouced\live\modules\Ouced\init.php"
    }
    ["function"]=>
    string(12) "include_once"
  }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-19 14:31 UTC] djbrainnrg at gmail dot com
Here is my phpinfo page
http://i.imgur.com/qLasmoY.png
 [2015-02-19 19:35 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2015-02-19 19:35 UTC] requinix@php.net
So what's in Request.class.php on line 198?

Needing "$_SERVER;" or needing to disable JIT suggests you have some sort of variable-variables stuff going on. PHP creates $_SERVER (and others) when it's detected in code so if there's some execution path where that doesn't happen before you try to do, say, $GLOBALS["_SERVER"], then you'll have this problem.
 [2015-02-20 20:55 UTC] djbrainnrg at gmail dot com
-Status: Feedback +Status: Open
 [2015-02-20 20:55 UTC] djbrainnrg at gmail dot com
Ah sorry, here the code that throws the error.

public function getHost(){
    return getValueFromArray($_SERVER, "HTTP_HOST");
}

and getValueFromArray is

function getValueFromArray(array $arr, $key){
    if(isset($arr[$key])){
        return $arr[$key];
    }
    $levels = explode("[", $key);
    $c = count($levels);
    foreach($levels as $k => $level){
        $level = trim($level, "[]");
        if(!isset($arr[$level])){
            return NULL;
        }
        $arr = $arr[$level];
    }
    return $arr;
}
 [2015-06-14 14:13 UTC] dominique at ottello dot net
Hi,
Windows 7 Pro 64 bits
Wamp with Apache 2.4.12 32 bits
PHP 7 Alpha 1 from http://windows.php.net/downloads/qa/php-7.0.0alpha1-Win32-VC14-x86.zip
The index.php file has always error :
[14-Jun-2015 08:56:44 UTC] PHP Notice:  Undefined variable: _SERVER in J:\wamp\www\index.php on line 25
Line 25 $server_software = $_SERVER['SERVER_SOFTWARE'];

I use $_SERVER['SERVER_SOFTWARE'] in another file : aviatechno/test/essais.php
echo "<p>SERVER_SOFTWARE   =".$_SERVER['SERVER_SOFTWARE']."| </p>\n";
without error.
 [2015-07-28 14:20 UTC] riggsfolly at gmail dot com
Ditto the report by (dominique at ottello dot net)

Except that :

Windows 7 Pro 32 bits
Wamp with Apache 2.4.9 32 bits
PHP 7 Beta 2 from http://windows.php.net/downloads/qa/php-7.0.0beta2-Win32-VC14-x86.zip

Odd error still exists, but a simple script like,

<?php
$server_software = $_SERVER['SERVER_SOFTWARE'];
echo $server_software;

Works every time with no errors reported.
 [2015-09-06 10:37 UTC] spl1nes dot com at googlemail dot com
I encountered this as well. It seems like the complexity of the script also plays a very important role. A one liner will never have this bug but a more complex/longer execution script can cause this bug.

I even did a var_dump($_SERVER); at the entry point of my application (without any prior modification) and in 2% of the executinos it resulted in a undefined variable notice. If I don't use a var_dump right at the entry point and access $_SERVER at a later point there is a higher chance of a undefined variable notice.

I highly assume this is related to auto_globals_jit = On. Once I turned it off I never had this problem.
 [2015-12-17 22:27 UTC] rm dot phpbt at romanrm dot net
I run into the same issue with _REQUEST.

The code below (from tt-rss):

---
<?php
...
	if (get_magic_quotes_gpc()) {
		function stripslashes_deep($value) {
			$value = is_array($value) ?
				array_map('stripslashes_deep', $value) : stripslashes($value);
				return $value;
		}

		$_POST = array_map('stripslashes_deep', $_POST);
		$_GET = array_map('stripslashes_deep', $_GET);
		$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
		$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
	}

	$op = $_REQUEST["op"];
---

always fails to work properly, with "PHP Notice:  Undefined variable: _REQUEST" in the last line.

However adding a simple line:

$_REQUEST;

into the beginning of the script does work-around the issue.
 [2017-07-12 15:20 UTC] ab@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ab
 [2017-07-12 15:20 UTC] ab@php.net
These issues are fixed in PHP 7.2. Please test the 7.2 pre releases and file a bug if you still experience these issues.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC