Bug #54131 [Com]: _SERVER, _ENV access via $$ shows Notice

From: Date: Wed, 02 Mar 2011 04:09:43 +0000
Subject: Bug #54131 [Com]: _SERVER, _ENV access via $$ shows Notice
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-159139@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54131&edit=1

 ID:                 54131
 Comment by:         daniel-marschall at viathinksoft dot de
 Reported by:        daniel-marschall at viathinksoft dot de
 Summary:            _SERVER, _ENV access via $$ shows Notice
 Status:             Open
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   Debian
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

I have found something additional.

(1) You can also "touch" the variable with simple
<?php
$_SERVER;
?>

(2) You do not have to touch the Variable BEFORE you want to access it
wit $$x. 
Actually, you just need to touch it ANYWHERE inside the script. Then,
the PHP 
interpreter will "activate" $_SERVER.

This scripts will throw an notice:

<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x);
?>

These scripts will not throw an notice

<?php
error_reporting(E_ALL | E_STRICT);
print_r(${'_SERVER'});
?>

<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x);
$_SERVER; // Touch anywhere in the script to avoid error
?>

I believe the interpreter checks if $_SERVER or $_ENV is called
somewhere in the 
script. If it is not called anywhere, it just don't load the variable.

In my opinion, this behavior is wrong. Since PHP cannot know what I do
with 
variables like $$x, it cannot know if I am actually trying to access
$_SERVER or 
$_ENV .

Solutions:

(1) Always "enable" _SERVER and _ENV

(2) When solving $$x , check if it is _SERVER or _ENV which is still
disabled 
and then enable it. Then, access it.


Previous Comments:
------------------------------------------------------------------------
[2011-03-02 04:52:11] daniel-marschall at viathinksoft dot de

Description:
------------
If you access $_ENV or $_SERVER the indirect way, via ${'_SERVER'} you
get an 
E_NOTICE message. Please see my test scripts.

Test script:
---------------
Producing the bug:

<?php
error_reporting(E_ALL | E_STRICT);
$x = '_SERVER';
print_r($$x); // Notice: Undefined variable: _SERVER
?>

Workaround with dummy function which "touches" the variable:

<?php
error_reporting(E_ALL | E_STRICT);

function dummy($dummy) {}
dummy($_SERVER); // "Touch"

$x = '_SERVER';
print_r($$x); // Everything is OK!
?>

Expected result:
----------------
I don't expect an message that $_SERVER or $_ENV are not valid
variables. I kept 
searching for hours because I couldn't find any reason, why $_SERVER
disappears, 
but is visible in phpinfo(). I do not know why I have to "touch" these
super-
globals first, so that they get "active".



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54131&edit=1


Thread (4 messages)

« previous php.bugs (#159139) next »