Page 1 of 1

[SHARING] Globals/Superglobals monitor

Posted: Wed Aug 22, 2007 8:20 am
by Defkon1
I'd like to post this function in the snippets area, but it's only moderator-writable...

i use this simple function to monitor some of globals/superglobals parameters in my web apps...

Code: Select all

function globals_monitor(){

$block = "<div class=\"debugBlock\"><h3>Globals monitor</h3>";

$cookie ='';
$session ='';
$post ='';
$get ='';

foreach($_COOKIE as $k => $v){
     ${$k} = $v;
     $cookie.="COOKIE['$k'] = $v<br/>";
}

$block.=$cookie;

foreach($_SESSION as $k => $v){
     ${$k} = $v;
     $session.="SESSION['$k'] = $v<br/>";
}

$block.=$session;

foreach($_POST as $k => $v){
     ${$k} = $v;
     $post.="POST['$k'] = $v<br/>";
}

$block.=$post;

foreach($_GET as $k => $v){
     ${$k} = $v;
     $get.="GET['$k'] = $v<br/>";
}

$block.=$get;
$block.="</div>";

return $block;
}

Code: Select all

.debugBlock{
	border: dashed 2px #ff0000
	color: #000000;
	font-family: arial, helvetica, sans-serif;
	margin: 10px 10px 10px 10px;
	text-align:left;
	padding: 0.5em;
	display:block;
}
not necessary, but useful, especially during debugging tests...

Posted: Wed Aug 22, 2007 8:29 am
by superdezign
... print_r()? var_dump()?

Why do you concatenate the strings after the foreach loops instead of during? And whats with the "${$k} = $v?" Is that a pseudo register_globals kind of thing going on?

Re: [SHARING] Globals/Superglobals monitor

Posted: Wed Aug 22, 2007 8:34 am
by The Phoenix
Defkon1 wrote:I'd like to post this function in the snippets area, but it's only moderator-writable...
That is so that others can help you refine your snippet, to make it well-designed code prior to being posted in the snippets area.

Think of it as a 'draft' system, where you submit drafts, revise, and once the mods think its shiny, it gets posted. :)

Posted: Wed Aug 22, 2007 8:56 am
by onion2k
superdezign wrote:And whats with the "${$k} = $v?" Is that a pseudo register_globals kind of thing going on?
Yes. It's a terrible idea, especially as it's not actually being used by the function.

Posted: Wed Aug 22, 2007 9:34 am
by CoderGoblin
I appreciate where you are coming from Defkon1 and thanks for the contribution. These variables do often need to be output when debugging, especially if you are unfamiliar with the code. However for most occasions a simple var_dump/print_r will suffice and you get used to reading it very quickly. I generally only place them in code when I get a problem/bug occuring, removing them immediately afterwards. Things like $_POST,$_GET and even $_COOKIE are normally a single level array and quite small. On the very rare instance where I would like a more "user friendly" view (multiple nested session variables is an example that springs to mind) I tend to use a collapsing tree display which I simply plug in (example). I feel this is justified (stress only rarely) as it adds additional functionailty compared with var_dump/print_r.

Posted: Wed Aug 22, 2007 1:56 pm
by Defkon1
:( i was so proud of that code... :(

but i promise that i will back with a new powerful snippet as soon as possible! :lol: :lol: :lol:
onion2k wrote:
superdezign wrote:And whats with the "${$k} = $v?" Is that a pseudo register_globals kind of thing going on?
Yes. It's a terrible idea, especially as it's not actually being used by the function.


why do you think it's terrible? (i'd like to improve my knowledge...)

Posted: Wed Aug 22, 2007 2:31 pm
by onion2k
Defkon1 wrote:why do you think it's terrible? (i'd like to improve my knowledge...)
Imagine you have a value in your $_SESSION array called 'id' and you convert it into $id to use it. If I go to page.php, but I added ?id=1 to the URL so I was viewing page.php?id=1, what happens? Your code has overwritten the $id value from $_SESSION with my value in $_GET. Fortunately in your script it'd only be visible in the scope of the function, but it's still a really bad idea. It's precisely why register_globals is switched off by default in php.ini these days.

Posted: Wed Aug 22, 2007 3:39 pm
by superdezign
CoderGoblin wrote:Things like $_POST,$_GET and even $_COOKIE are normally a single level array and quite small.
On that note, the code doesn't put array data into consideration at all. It'd just print 'Array.'

Posted: Wed Aug 22, 2007 4:28 pm
by kaszu
Also if there will be any html tags in the input, then they won't be printed correctly.

Posted: Thu Aug 23, 2007 3:43 am
by Defkon1
thanks all for all this info/fixes/tips! :D