Are there any tools out there that will let me edit the contents of my $_SESSION? I'm trying to debug a script that is dependant on session state and I'd like to just be able to change the session variables rather than have to change the database, destroy the session and recreate it. I could probably build an ad-hoc session editor given time, but I don't have time to spare at the moment.
Well the Information in $_SESSION is just stored as a serialized string on the disk. (If you don't use something like memcached session storage)
So reading in that file, unserializing it's contents, changing the appropriate values and then serializing it back should be pretty much everything you need.
If you don't want to deal with that you could set the session_id() before session_start(), then edit the values using php and then call session_write_close() to store it on disk again.
Sample script for the session id:
<?php
session_id("838c4dc18f6535cb90a9c2e0ec92bad4");
session_start();
var_dump($_SESSION);
$_SESSION["a"] = "foo";
session_write_close();
Sample script for the unserialisation (function taken from the comments on the unserialze php.net page)
<?php
session_save_path("./session");
session_start();
$_SESSION["x"] = 1;
$id = session_id();
var_dump($id);
session_write_close();
$session = file_get_contents("./session/sess_$id");
var_dump($session);
function unserialize_session_data( $serialized_string ) {
$variables = array( );
$a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
for( $i = 0; $i < count( $a ); $i = $i+2 ) {
$variables[$a[$i]] = unserialize( $a[$i+1] );
}
return( $variables );
}
var_dump(unserialize_session_data($session));
Putting it back together isn't hard ether.
To expand it slightly, just adding the ability to add new session vars:
<?php
function listData (array $data, array $parents = array ())
{
$output = '';
$parents = array_map ('htmlspecialchars', $parents);
$fieldName = $parents?
'[' . implode ('][', $parents) . ']':
'';
foreach ($data as $key => $item)
{
$isArr = is_array ($item);
$output .= $isArr?
'<li><h4>' . htmlspecialchars ($key) . '</h4>':
'<li><label>' . htmlspecialchars ($key) . '</label>: ';
$output .= $isArr?
'<ul>' . listData ($item, array_merge ($parents, array ($key))) . '</ul>':
'<input type="text" name="fields' . $fieldName . '[' . htmlspecialchars ($key) . ']" value="' . htmlspecialchars ($item) . '" />';
$output .= "</li>\n";
}
return ($output);
}
session_start ();
if ($_POST ['fields'])
{
$_SESSION = $_POST ['fields'];
session_commit ();
}
if ($_POST['newfield'])
{
$_SESSION[$_POST['newfield']] = $_POST['newfieldvalue'];
session_commit ();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Session Editor</title>
<style type="text/css">
label {
display: inline-block;
min-width: 8em;
text-align: right;
padding-right: .3em;
}
</style>
</head>
<body>
<h2>Session Editor</h2>
<form action="<?php echo ($_SERVER ['SCRIPT_NAME']); ?>" method="post">
<ul>
<?php echo (listData($_SESSION)); ?>
</ul>
<div>
<input type="submit" />
</div>
</form>
-------------------------
<form action="<?php echo ($_SERVER ['SCRIPT_NAME']); ?>" method="post">
New Session Var:<input type="text" name="newfield" /><br />
Session Var Value:<input type="text" name="newfieldvalue" />
<div>
<input type="submit" />
</div>
</form>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With