1

I'm trying to use the Python phpserialize library to read serialized session data from PHP. However, I discovered that my PHP setup is saving session data in a format that is different from what phpserialize (and pretty much all other documentation across the web) expects. For example:

$_SESSION["userid"] = 42;
echo session_encode();

prints out

userid|i:42;

whereas phpserialize serializes this as:

phpserialize.serialize({'userid':42})
'a:1:{s:6:"userid";i:42;}'

I checked the session.serialize_handler through phpinfo() and it's set to 'php' (the other options being php_binary and wddx). I cannot use any of the standard php serialization libraries in python as a result. Any suggestions?

6
  • 1
    session_encode - Please note the serialization method is not the same as serialize(). The serialization method is internal to PHP can can be set using session.serialize_handler. Commented Aug 13, 2012 at 14:50
  • try json_encode php.net/manual/en/function.json-encode.php Commented Aug 13, 2012 at 14:51
  • @decereé I'm not using the php serialize() function anywhere, but need to know what I have to do to have session_encode() in php and python phpserialize.unserialize() to be able to talk to each other. Commented Aug 13, 2012 at 14:56
  • serialize is the PHP serialization format that phpserialize emulates. The above is saying that session data is saved in another format that is not the same as the PHP serialization format. Commented Aug 13, 2012 at 14:58
  • possible duplicate of Looking for spec. of PHP Internal session format in order to share session information between PHP and another framework Commented Aug 13, 2012 at 15:23

2 Answers 2

3

You should be calling serialize on the $_SESSION array if you want to pass this from PHP to Python.

Example:

echo serialize($_SESSION);

The result will then be interpreted by the PHP serialization lib.

session_encode is NOT the same as serialize.

Sign up to request clarification or add additional context in comments.

Comments

1

On PHP you serialize variable and in Python you serialize array.

This code:

$data = array('userid' => 42);
echo json_encode($data);

will show you the same result as phpserialize do.

But it's better to use json_encode/json_decode to transfer data between php application & python application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.