Page 1 of 1

Fuzzy global variables

Posted: Sun Apr 17, 2005 10:37 pm
by Roja
I'd like to get an array populated with variables that *are* global, but which are not part of:

Code: Select all

{$HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_COOKIE_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $_POST, $_GET, $_REQUEST, $_COOKIE, $_FILES, $_ENV, $_SERVER, $_SESSION}
The idea being that if you set a variable like so:

Code: Select all

global $whatever;
$whatever = 'testing';
It would be in the $global_array. Then I can send that out to a var_dump like function, and see what variables are in the global scope, without repetition from the superglobal arrays.

I'm trying to build the script to work with Data Dumpr: http://www.razormotion.com/software/dumpr/

So far, I have a working script for dumping out all the superglobals, other than $GLOBALS.

Every attempt I've made seems to result in either some level of repetition, or in $whatever also being removed. In both cases, that doesnt solve the problem.


---
My current disgusting/incorrect code follows:

Code: Select all

<?php
include '[url=http://www.razormotion.com/software/dumpr/]dumpr.php[/url]';
global $test;
$test = 'testing!';

global $whatever;
$whatever = array('something'=>'is something','another'=>'is another');

if (!empty($_ENV))
{
    echo "Environment variables<br>";
    dumpr($_ENV);
    echo "<br>";
}

if (!empty($_SERVER))
{
    echo "Server variables<br>";
    dumpr($_SERVER);
    echo "<br>";
}

if (!empty($_SESSION))
{
    echo "Session variables<br>";
    dumpr($_SESSION);
    echo "<br>";
}

if (!empty($_GET))
{
    echo "Get variables<br>";
    dumpr($_GET);
    echo "<br>";
}

if (!empty($_POST))
{
    echo "Post variables<br>";
    dumpr($_POST);
    echo "<br>";
}

if (!empty($_COOKIE))
{
    echo "Cookie variables<br>";
    dumpr($_COOKIE);
    echo "<br>";
}

if (!empty($_FILES))
{
    echo "File variables<br>";
    dumpr($_FILES);
    echo "<br>";
}
if (!empty($_REQUEST))
{
    echo "Request variables<br>";
    dumpr($_REQUEST);
    echo "<br>";
}

$global_array = $GLOBALS;
$global_array = array_diff($global_array, $_ENV);
//$global_array = array_diff($global_array, $_SERVER); 
// Adding this removes the server variables, but also removes the vars we need. 

foreach($global_array as $key=>$value)
{
    if ($key == 'key' || $key == 'value') // Dont list key itself.
    {
        unset ($global_array['key']);
        unset ($global_array['value']);
    }
}

if (!empty($global_array))
{
    echo "Global variables<br>";
    dumpr($global_array);
    echo "<br>";
}
?>
Any help is deeply appreciated.

Posted: Mon Apr 18, 2005 1:54 pm
by trukfixer
try this. might get you what you need..

Code: Select all

<?php
ini_set('error_reporting',"E_ALL");
$testing = (int)3;
$horde="hello";
$GLOBALS[1] = "yellow";
global $testing,$db,$fun;



function get_global_vars($item,$key)
{
     if(!is_array($item))
     {
        echo "$key = $item<br>";
     }
}

$final = array_walk($GLOBALS,"get_global_vars");


 ?>
outputs:

Code: Select all

testing = 3
horde = hello
1 = yellow
db =
fun =

Posted: Mon Apr 18, 2005 2:19 pm
by trukfixer
OK.. new code puts it out as an array so you can use dumpr stuff :)

CODE:

Code: Select all

<?php
ini_set('error_reporting',"E_ALL");
$testing = (int)3;
$horde="hello";
$GLOBALS[1] = "yellow";
global $testing,$db,$fun;


$value = array();
function get_global_vars($item,$key)
{
    global $value;
     if(!is_array($item) && $key != 'value')
     {
        array_push($value[$key],$item);
     }
}
$global_arr = $GLOBALS;

array_walk($global_arr,"get_global_vars");

echo"<pre>";
var_dump($value);
 ?>
OUTPUT:

Code: Select all

array(5) {
  &#1111;&quote;testing&quote;]=&gt;
  NULL
  &#1111;&quote;horde&quote;]=&gt;
  NULL
  &#1111;1]=&gt;
  NULL
  &#1111;&quote;db&quote;]=&gt;
  NULL
  &#1111;&quote;fun&quote;]=&gt;
  NULL
}
note that $GLOBALS[1] returns NULL instead of "yellow" as set above- this is because array_walk ignores any element that is itself an array... it only displays the single elements of $GLOBALS that are global *variables* .. global arrays and objects arent included in the above code..

looks like its about as good as it can get..

the problem is when something is SET global, $GLOBALS is a copy of itself, (it references itself)

if you make a copy of $GLOBALS to another value, that value takes on identical properties to $GLOBALS , and therefore, the value name is itself global..

it's a bit tricky but I'll keep digging around to see if I can get global arrays that arent a copy of the $GLOBALS array ($GLOBALS['GLOBALS'])

however, for your purposes, it would seem that you can dumpr the arrays themselves, whether they are global or not.. unless I misunderstand your needs.

Posted: Mon Apr 18, 2005 3:34 pm
by mudvein
or you could just look at the user responses at the bottom of http://www.php.net/global and get the same code with a better explanation...

Posted: Mon Apr 18, 2005 4:14 pm
by Roja
mudvein wrote:or you could just look at the user responses at the bottom of http://www.php.net/global and get the same code with a better explanation...
Would you mind being more specific about which user responses you think fit this problem? There are 35 postings, and only one is (as far as I can tell) actually close to the same issue. That one is about parsing a FILE, not checking the variables that are actually in memory.

I'm hopeful that I missed a user comment, and that you meant to be helpful, but the phrasing you used really just amounts to "RTFM", and I reply that I have, and dont see the answer to the specific question I'm asking. :)

Posted: Mon Apr 18, 2005 4:18 pm
by Roja
trukfixer wrote: note that $GLOBALS[1] returns NULL instead of "yellow" as set above- this is because array_walk ignores any element that is itself an array...
Yeah, thats not an acceptable solution. I'd rather have too much information that not enough. Note that the version I posted gives all the global variables (even arrays), which is "more correct" for the problem scope.

Specifically, we need to be able to use the output as the equivalent of a debugger, listing all variables in use.
trukfixer wrote: it only displays the single elements of $GLOBALS that are global *variables* .. global arrays and objects arent included in the above code..
Yeah, thats the problem I ran into - I could either get *EVERYTHING* (which included mountains of repetition), or your version, which left out critical items.
trukfixer wrote: looks like its about as good as it can get..
I can't accept that as the final solution - there is a way, I just dont see it yet.
trukfixer wrote: it's a bit tricky but I'll keep digging around to see if I can get global arrays that arent a copy of the $GLOBALS array ($GLOBALS['GLOBALS'])
Thats the path I took as well, and unfortunately, havent found success in yet.
trukfixer wrote: however, for your purposes, it would seem that you can dumpr the arrays themselves, whether they are global or not.. unless I misunderstand your needs.
I'd need a method that would detect each of the arrays, and then dump them, which puts us back at that same problem of listing all global elements without getting the duplicate/recursive data.