PHP – Code Snippet – a Quick Variable Checker

When your writing a controller you often ask yourself whether menu choices and form data are getting to the controller file without any errors, are the variables set up properly, and are the form selects selecting properly.

And you have your $_SESSION variables. Is the one variable you put in the $_SESSION variable three files ago still there?

This often leads you to making a series of echo statements, where you jump back and form from the form file to the controller file checking if the variable is good, or you misspelled a name. This is time consuming, and sometimes frustrating when creating a new form with a lot of entries.

Most of the time you endure, because your into that moment, and the thought of stopping to think of an easier way does not enter your mind.

With this little snippet of code, you can do all your checking with one line of code, with a simple include file, which will tell you everything you want to know. This allows you to fix every problem encountered with passing variables, and when everything works correctly, you simply remove the one include line, and you've finished your variable checking.

Here's a simple piece of code, which loops through your $_COOKIE array, your $_SESSION array, your $_GET array and your $_POST array, listing each variable name and their corresponding value currently available to the page. There is nothing complicated here, just simple foreach loops looping through each array. Just put the file in your application, and call it when needed with an include.

Here's the code. Save it to a file, like "var_check.php" for example, and put it in a folder in your application.

function var_check()
{
echo "VARIABLES IN YOUR GLOBAL ARRAYS ";
echo "These are the current COOKIE variables ";
    foreach( $_COOKIE as $key_name => $key_value)
    {
	echo $key_name . " = " . $key_value . "" ;
    }

echo "These are your current SESSION variables:" ;
    foreach( $_SESSION as $key_name => $key_value)
    {
	echo $key_name . " = " . $key_value . "" ;
    }

echo "These are your current GET variables:";
    foreach( $_GET as $key_name => $key_value)
    {
	echo $key_name . " = " . $key_value . "";
    }

echo "These are your current POST variables: " ;
    foreach( $_POST as $key_name => $key_value)
    {
	echo $key_name . " = " . $key_value . "";
    }
}  // end of the var_check function

function server_check()
{
    echo "These are your SERVER variables: " ;
    foreach( $_SERVER as $key_name => $key_value)
    {
	echo $key_name . " = " . $key_value . "";
    }
}  // end of the server_check function

var_check();

You'll notice, I call the function at the end of the include file, so you don't have to include the function call as a separate line when you drop it in your file.

Here's the one line that you put at the top of your controller to list all your variables.

include("../files/test/var_check.php");

Here's the output of the function call.

VARIABLES IN YOUR GLOBAL ARRAYS
These are the current COOKIE variables
PHPSESSID = dpo4glbo7314hs3frr6bvd2fu1

These are your current SESSION variables:
username = dale
user_id = 2
login_status = OK
LOGIN_MESSAGE =
news_id =

These are your current GET variables:

These are your current POST variables:
projname = dsafdsaf
dept = 3
prjyear = 2010
description = dsaf ad fads
stat = 5
lead = 2
statnote = dasfds a dfsadf
execorder = 11
cgoal = 2
ngoal = 3
mandates = adfa
objectives = dafdsf
measures = dsaf  dsfa  sadf
comms = dsfds adsf asddf
keymess = sdafsda asddfsad
tools = adfsd
saveprj_mile = save project & add a milestone

To check each one of these variables separately would involve a line of code something like this example:

echo "this checks the stat variable: " . $_POST['stat']  ;

Then you would have to remember the value you set "stat" to in your form. So this could save you quite a bit of time going back and forth between the view and controller files.

As an added bonus I included a function you can call separately called, "server_check()" which will loop through your server globals and show you the information available there.

Here's how to use the server_check function in your file.

include("../files/config/var_check.php");
server_check();

Here's the sample output from the server_check() function.

HTTP_HOST = localhost
HTTP_USER_AGENT = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5
HTTP_ACCEPT_ENCODING = gzip,deflate
HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_KEEP_ALIVE = 115
HTTP_CONNECTION = keep-alive
HTTP_REFERER = http://localhost/intranet3/views/pp_new.php
HTTP_COOKIE = PHPSESSID=dpo4glbo7314hs3frr6bvd2fu1
HTTP_CACHE_CONTROL = max-age=0
CONTENT_TYPE = application/x-www-form-urlencoded
CONTENT_LENGTH = 205
PATH = c:\Program Files\NVIDIA Corporation\PhysX\Common;C:\wamp\bin\php\php5.3.0\;C:\Program Files\ActiveState Komodo Edit 5\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Git\cmd;C:\Program Files\Git\bin
SystemRoot = C:\WINDOWS
COMSPEC = C:\WINDOWS\system32\cmd.exe
PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
WINDIR = C:\WINDOWS
SERVER_SIGNATURE =
SERVER_SOFTWARE = Apache/2.2.11 (Win32) PHP/5.3.0
SERVER_NAME = localhost
SERVER_ADDR = 224.10.0.1
SERVER_PORT = 80
REMOTE_ADDR = 224.10.0.1
DOCUMENT_ROOT = C:/wamp/www/
SERVER_ADMIN = admin@localhost
SCRIPT_FILENAME = C:/wamp/www/intranet3/rules/ru_pp_new.php
REMOTE_PORT = 2247
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
REQUEST_METHOD = POST
QUERY_STRING =
REQUEST_URI = /intranet3/rules/ru_pp_new.php
SCRIPT_NAME = /intranet3/rules/ru_pp_new.php
PHP_SELF = /intranet3/rules/ru_pp_new.php
REQUEST_TIME = 1278368120

That's about it. Enjoy.

Comments are closed.