Facing problem with PHP unserialize()
function as titled it is throwing error.
unserialize() [function.unserialize]: Error at offset 0 of 1781 b
try Read Session Data from Session File
Use this class:
<?php
class Session {
public static function unserialize($session_data) {
$method = ini_get("session.serialize_handler");
switch ($method) {
case "php":
return self::unserialize_php($session_data);
break;
case "php_binary":
return self::unserialize_phpbinary($session_data);
break;
default:
throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
}
}
private static function unserialize_php($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
if (!strstr(substr($session_data, $offset), "|")) {
throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
}
$pos = strpos($session_data, "|", $offset);
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
private static function unserialize_phpbinary($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
$num = ord($session_data[$offset]);
$offset += 1;
$varname = substr($session_data, $offset, $num);
$offset += $num;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
}
?>
Usage:
<?php
Session::unserialize(file_get_contents($sessionSavePath."/".$sessionFileName);
?>
Thats Work!
Following can be a way to read session data from session file
//$file='/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$sSessId = 'ciktos8icvk11grtpkj3u610o3';
session_id($sSessId);
session_start();
print_r($_SESSION);
If you want to decode session data, use session_decode
(see the manual). unserialize
only decodes single variables, not session data.
You can do something like:
$file = '/var/www/html/products/var/session/sess_ciktos8icvk11grtpkj3u610o3';
$contents = file_get_contents($file);
session_start();
session_decode($contents);
print_r($_SESSION);
check out, this might click you something
function read($filename)
{
session_save_path("/tmp/tst");
session_start();
echo $sCurrentFile = "/tmp/tst/sess_".session_id();
$sFileToRead = $filename;
if( !file_exists($sFileToRead) || !$sessionData=(string)@file_get_contents($sFileToRead) )
{
echo "file does not exist";
}
$fh = fopen($sCurrentFile, 'w') or die("can't open file");
fwrite($fh, $sessionData);
fclose($fh);
$_SESSION["mytest"] = 444;
print_r($_SESSION);
}
That is not legal PHP serialized data, that's PHP session data.
PHP session data uses the serialized format internally, but it is not serialized data itself.
The only thing that can safely and sanely read session data is PHP's session code. It is sometimes possible to read it using a regular expression and some creative editing, but you can not rely upon those methods.
If you need data out of a user's session, your best bet is to write a custom session wrapper and let it do the work when the data itself changes rather than try and work with the data after the fact.
(I'm not talking about custom session-writing code, I'm talking about a class that you would use instead of using $_SESSION
directly.)