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开发者_如何学Python, 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>
Source code for an extremely basic session editor (yes, found a little time to actually work on one).
<?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 ();
}
?>
<!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>
</body>
</html>
Obviously extremely simplistic, but it does at least allow me to edit arbitrary session data without having to write new code every time. Might work some more on this at some point to build it into a fully featured editor, but it will do for now.
any tools out there that will let me edit the contents of my $_SESSION?
$_SESSION['var']="whatever value";
var_dump( $_SESSION );
to see your session
and
$_SESSION['variabletoset'] = 'value';
to sett your session
You generally want to var_dump the session to see what it is when you debug it.
Like Col. Shrapnel mentions I would simply setup a new PHP script on the same server that looked like this:
<?php
session_start();
$_SESSION['key'] = 'value'; // repeat for every value you need to change
Then execute the script every time you need to update the session variables. Just have your app open in one browser tab and the session update script in another. Simple.
I would never bother with tampering in the session temp files myself.
You can also do it in real-time in your debug session if you are using PHPEdit
精彩评论