Parse PHP Session in Javascript

前端 未结 4 808
太阳男子
太阳男子 2021-02-08 12:07

I have recently gone into extending my site using node.js and have come to realisation I need a session handler for my PHP sessions. Now everything was cool and dandy and node.j

相关标签:
4条回答
  • 2021-02-08 12:43

    I'm relatively sure PHP uses the serialize and unserialize functions to handle session data.

    There is a JavaScript implementation of unserialize in PHP.JS, you can find it here: http://phpjs.org/functions/unserialize

    0 讨论(0)
  • 2021-02-08 12:50

    Just to reply with the way I've found: force PHP to use JSON instead :

    Use the class below, with session_set_save_handler to store data as JSON and let PHP still using his own system. Link this class using this function at the beginning of each script you use session : http://php.net/manual/ru/function.session-set-save-handler.php

    PS : here the class use memcache to store JSON resulting object, on Node.JS, use a memcache object, and retrieve like this :

    • get the PHPSESSID from cookies
    • use that characters string to bring a key from memcached with "sessions/id" where id is the key and session just the string (see class below).

    Now you should have only JSON in memcache, so it's becoming a simple game with Node.JS to work with.

    PS : of course you can work with/improve this, you can also use this not only with memcache (a db for example)

    <?php
    /**
     * This class is used to store session data with memcache, it store in json the session to be used more easily in Node.JS
     */
    class memcacheSessionHandler{
        private static $lifetime = 0;
        private static $memcache = null;
    
        public function __construct(){
                self::$memcache = new Memcache();
                self::$memcache->addServer('localhost', 11211);
    }
    
        public function __destruct(){
                session_write_close();
                self::$memcache->close();
                self::$memcache = null;
        }
    
        public static function open(){
                self::$lifetime = ini_get('session.gc_maxlifetime');
                return true;
        }
    
        public static function read($id){
                $tmp = $_SESSION;
                $_SESSION = json_decode(self::$memcache->get("sessions/{$id}"), true);
                $new_data = session_encode();
                $_SESSION = $tmp;
                return $new_data;
        }
    
        public static function write($id, $data){
                $tmp = $_SESSION;
                session_decode($data);
                $new_data = $_SESSION;
                $_SESSION = $tmp;
                return self::$memcache->set("sessions/{$id}", json_encode($new_data), 0, self::$lifetime);
        }
    
        public static function destroy($id){
                return self::$memcache->delete("sessions/{$id}");
        }
    
        public static function gc(){
                return true;
        }
    
        public static function close(){
                return true;
        }
    }
    ?>
    
    0 讨论(0)
  • 2021-02-08 12:51

    Here is a session_decode function based on the unserialize function of phpjs: https://github.com/vianneyb/phpjs/blob/master/functions/var/session_decode.js

    works for me!

    0 讨论(0)
  • 2021-02-08 12:58

    I had the same problem. I needed to integrate php with node.js. I used js-php-unserialize to do this. The use is pretty straightforward.

    var PHPUnserialize = require('php-unserialize');
    console.log(PHPUnserialize.unserializeSession(yourData));
    

    For example if you

    var yourData = 'A|s:1:"B";userID|s:24:"53d620475e746b37648b4567";';
    

    you will get this result:

    {
      A: 'B',
      userID: '53d620475e746b37648b4567'
    }
    
    0 讨论(0)
提交回复
热议问题