Skip to content

User Session Serializer #2205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from

Conversation

yohgaki
Copy link
Contributor

@yohgaki yohgaki commented Nov 17, 2016

This PR is to add user defined session serializer.
User defined session serializer can be used

  • to encrypt/decrypt session data
  • to serialize session data as JSON/XML/etc
  • to add hidden management data for session

This PR is required for deprecating current OO style session save handler and implement new OO style session save handler that does not have any base class. i.e. Do not create and use currently used internal save handler as base class. This will eliminate many kinds of session save handler abuses.

@php-pulls
Copy link

Comment on behalf of krakjoe at php.net:

labelling

@php-pulls php-pulls added the RFC label Nov 17, 2016
@krakjoe
Copy link
Member

krakjoe commented Nov 17, 2016

When this is ready for discussion, please squash the commits and use a sensible log message ... the log is very noisy ...

@@ -86,5 +86,5 @@ session_start();
bool(true)

Warning: session_set_save_handler() expects parameter 1 to be SessionHandlerInterface, object given in %s
bool(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should the feature for user defined serialized affect the return value of this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must return NULL because perse parameter is failing. Not sure why older code produces bool(false). I'll check it later.

@krakjoe
Copy link
Member

krakjoe commented Feb 13, 2017

The RFC was declined, closing.

@krakjoe krakjoe closed this Feb 13, 2017
@nolimitdev
Copy link

nolimitdev commented Aug 14, 2018

Reason for this PR is mentioned by yohgaki here: https://gist.github.com/yohgaki/432579e535ae97856a1227e4d47d0e2e
But I think we do not have to use serialize() and unserialize() to store JSON in session file. See my solution...

class MyHandler extends SessionHandler {

    function read($session_id) {
        $content = parent::read($session_id);
        if (!empty($content) && ($json_decode = json_decode($content, true)) !== null) {
            $_SESSION = $json_decode;
            return '';
        }

        return $content;
    }

    function write($session_id, $session_data) {
        if (empty($_SESSION) || ($json_encode = json_encode($_SESSION, JSON_UNESCAPED_UNICODE)) === false) {
            $json_encode = '';
        }

        return parent::write($session_id, $json_encode);
    }
}

Advantages of this solution:

  • no overhead using serialize() and unserialize() as in https://gist.github.com/yohgaki/432579e535ae97856a1227e4d47d0e2e
  • independent on session.serialize_handler ("php_serialize" is not required because of omitting serialize and unserialize... "php_serialize" is not available before PHP 5.5.4)
  • when using internal "php" (not compatible with serialize and unserialize functions) as session.serialize_handler we can use special characters (| and !) in indexes of $_SESSION
  • deploying this code first time on production will not break existing sessions (thanks to return $content when decoding as JSON fails)

... so I do not thing that something like suggested session_set_save_handler() is needed

@yohgaki
Copy link
Contributor Author

yohgaki commented Aug 18, 2018

@nolimitdev
You didn't realize that session module ALWAYS serialize by current serializer.

Therefore, your code simply don't optimize anything without user defined serializer.

@yohgaki
Copy link
Contributor Author

yohgaki commented Aug 18, 2018

Anyway, hacks are not good way for solutions.
Modularized approach is better always.

@yohgaki
Copy link
Contributor Author

yohgaki commented Aug 18, 2018

@nolimitdev I read your code again. You should read session.c before comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants