PDA

View Full Version : [PHP] Simple function to help with debugging



DarkAz
February 7th, 2008, 05:52 PM
It seems that a lot of the PHP-related problems posted on this forum are easily fixed with some simple environment/global variable analysis

here's a simple routine that you can add to your code to display these and make things a bit easier

note you will need to ensure your script calls session_start() before the debugInfo() function if you want to view the current session variables


<?php

@session_start();

function debugInfo() {
// echo out the optional variables
if (count($_POST)) {
echo "<h1>POST Variables</h1>\n";
echo "<pre>" . print_r($_POST, true) . "</pre>\n";
}
if (count($_GET)) {
echo "<h1>GET Variables</h1>\n";
echo "<pre>" . print_r($_GET, true) . "</pre>\n";
}
if (count($_SESSION)) {
echo "<h1>SESSION Variables</h1>\n";
echo "<pre>" . print_r($_SESSION, true) . "</pre>\n";
}
if (count($_COOKIE)) {
echo "<h1>COOKIE Variables</h1>\n";
echo "<pre>" . print_r($_COOKIE, true) . "</pre>\n";
}
if (count($_REQUEST)) {
echo "<h1>REQUEST Variables</h1>\n";
echo "<pre>" . print_r($_REQUEST, true) . "</pre>\n";
}

// echo out the server variables (always exist)
echo "<h1>SERVER Variables</h1>\n";
echo "<pre>" . print_r($_SERVER, true) . "</pre>\n";
}

?>

Hopefully this will be of some use