[SHARING] Globals/Superglobals monitor

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

[SHARING] Globals/Superglobals monitor

Post 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...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
User avatar
The Phoenix
Forum Contributor
Posts: 294
Joined: Fri Oct 06, 2006 8:12 pm

Re: [SHARING] Globals/Superglobals monitor

Post 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. :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post 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...)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.'
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Also if there will be any html tags in the input, then they won't be printed correctly.
Defkon1
Forum Newbie
Posts: 19
Joined: Thu Aug 09, 2007 9:13 am

Post by Defkon1 »

thanks all for all this info/fixes/tips! :D
Post Reply