This topic is locked

Debugging with Session Variables

3/13/2010 1:49:13 AM
PHPRunner Tips and Tricks
A
alang author

The $_SESSION variables are used quite extensively by PHPR and I have worked out a way to easily view or edit these variables on any page which others might find useful. The technique uses the AJAX code already used in PHPR to show pop-ups of child records on the master table list page.
There are three parts to it as follows:

  1. Header Code. This can be put into a source code file - say hdr.php. This contains the hyperlinks and javascript code to be included in the header.



<script type='text/javascript' src='include/jquery.js'></script>

<script type='text/javascript' src='include/ajaxsuggest.js'></script>

<div id='master_details' onmouseover='RollDetailsLink.showPopup();' onmouseout='RollDetailsLink.hidePopup();'></div>

<?php

$selfs = explode("/",$_SERVER["PHP_SELF"]);

$levels = count($selfs);

$pgs = explode(".",$selfs[$levels-1]); // strip filename and extension

$pg = $pgs[0]; // This is page with possible extension added by PHPRunner

$subpg = strrchr($pg,'_');

if($subpg !== false)

$pg = substr($pg,0,-strlen($subpg));

echo "Display Session Vars: &nbsp";

echo "<A class=tablelinks href=\"debug.php?dbgcmd=0&dbgval=all\"

onmouseover=\"RollDetailsLink.showPopup(this,'debug.php'+this.href.substr(this.href.indexOf('?')));\"

onmouseout=\"RollDetailsLink.hidePopup();\">ALL</A>&nbsp&nbsp";

echo "<A class=tablelinks href=\"debug.php?dbgcmd=0&dbgval=$pg\"

onmouseover=\"RollDetailsLink.showPopup(this,'debug.php'+this.href.substr(this.href.indexOf('?')));\"

onmouseout=\"RollDetailsLink.hidePopup();\">Page</A>&nbsp&nbsp";

echo "<A class=tablelinks href=\"debug.php?dbgcmd=0&dbgval=@\"

onmouseover=\"RollDetailsLink.showPopup(this,'debug.php'+this.href.substr(this.href.indexOf('?')));\"

onmouseout=\"RollDetailsLink.hidePopup();\">Temp</A>&nbsp&nbsp;&nbsp;&nbsp;";

echo "<A style='color:red' href=\"javascript:var setstr=prompt('Enter assignment command:','{Session var}={new value}');

RollDetailsLink.showPopup(this,'debug.php?dbgcmd=1&dbgval='+setstr);\">Set Session Var</a>&nbsp;&nbsp;&nbsp;";

echo "<A style='color:red' href=\"javascript:RollDetailsLink.showPopup(this,'debug.php?dbgcmd=2');\">Clear Temp Vars</a>";

?>


2) Debug Code module. This is similar to the PHPR preview file and generates the content of the pop-up windows.



<?php

ini_set("display_errors","1");

ini_set("display_startup_errors","1");

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");

set_magic_quotes_runtime(0);
include("include/dbcommon.php");
// Parameters define what the debug module will do

// 0 or NULL - display SESSION vars

// 1 - Set session variable to any value

// 2 - Clear all temporary variables (starting with '@' character)
$dbgact = $_REQUEST["dbgcmd"];

$dbgparm = $_REQUEST["dbgval"];
// Compares display parameter to level 1 SESSION key and returns true/false

// depending on whether it should be included in display

function dmatch($ks)

{

global $otherexcludes, $dbgparm, $tabexpand;
$filt = strtoupper($dbgparm);

if($filt == "ALL")

return true;

if(!strncasecmp($ks,$filt,strlen($filt)))

return true;

return false;

}

// Here on display - loop each value in $_SESSION

if(!$dbgact)

{

echo "<B>Displaying SESSION parameters: $dbgparm</B>";

$ts = $_SESSION; // temporary variable

ksort($ts);

$kcnt = 0;

echo "<table cellpadding=1 cellspacing=1 border=0 align=left class=\"detailtable\"><tr><td>";

foreach($ts as $k1=>$v1)

{

if(dmatch($k1))

{

$kcnt++;

echo $k1."</TD><TD>"; // outer key in column 1

print_r($v1);

echo "</TD></TR><TD>"; // end row and ready in first column

}

}

if(!$kcnt)

echo "No matching records found.";

echo "</TD></TR>"; // last row blank

echo "</TABLE>";

}
// Check for action rather than default display for what to do

if($dbgact == 1)

{

$bits = explode("=",$dbgparm); // separate name from value

if($bits[0])

$_SESSION[$bits[0]] = $bits[1];

}
if($dbgact == 2)

{

foreach($_SESSION as $kv=>$d)

if(substr($kv,0,1)=="@")

unset($_SESSION[$kv]);

}

echo "counterSeparator".postvalue("counter");

?>


3) Link the code in from the header. On the visual editor tab, click on Header, switch to HTML mode and insert the following code:



<?php include "hdr.php" ?>


In the example code above, there are five hyperlinks, the first three show $_SESSION variables (All, related to current page, and temporary) where I define temporary variables as starting with the "@" character. The last two links are action links and need to be clicked on. The first allows you to set a $_SESSION variable to any value, and the last "unsets" any temporary variables.
The two extra files (hdr.php and debug.php) can be put in the source folder of the PHPR project or copied directly to the top level of the generated code.
Enjoy!

A
alang author 4/26/2010

Note: For PHPR V5.2 remove lines in hdr.php prior to the "<?php" line. Having them there stops inline functions from working.