Viewed   131 times

Possible Duplicate:
How to read/make sense of a PHP serialised data string in python

I'm using python to access a database which is managed by a Drupal install. The data I want to access in Drupal in saved in the database as a PHP serialized object.

Are there any pre-built python modules which can deserialize a PHP serialized object into a python object? I've done some searching and come up with nothing.

I realize I could write my own parser from scratch but I'd rather use something thats been tried and tested.



Are you looking for phpserialize?

Tuesday, October 11, 2022

The PHP serialize and unserialize functions can not be used to serialize and unserialize session data. Even if (by default - and only by default) the serialization might look similar, there is an important difference to those two functions that care about a single variable contents only:

Those [sessions] are a list of serialized values with their variable name.

(from: Serialized README)

So you would need to create your own a session_unserialize function that is able to decode the string (e.g. via session_decode) which is returned from your database. Take care that this needs everything in there, e.g. if the session contains serialized objects, the class definitions needs to be loaded.

An exemplary session_unserialize function could look like (adopted from: a session related answer):

function unserialize_session($data) {
    $hasBuffer = isset($_SESSION);
    $hasBuffer && $buffer = $_SESSION;
    $session = $_SESSION;
    $hasBuffer ? $_SESSION = $buffer : unset($_SESSION);
    return $session;
Saturday, October 15, 2022

Using PDO prepared statements:

$placeholders = str_repeat('?, ', count($array)-1) . '?';
$stmt = $pdo->prepare("SELECT * FROM table WHERE field IN ($placeholders)");

$placeholders will contain a sequence of ?, ?, ? placeholders, with the same number of ? as the size of the array. Then when you execute the statement, the array values are bound to the placeholders.

Saturday, August 27, 2022

Use .unserializeSession() instead of .unserialize().

Friday, September 16, 2022

No. When a process is killed, the operating system releases all operating system resources (memory, sockets, file handles, …) previously acquired by that process.

Sunday, September 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :