What is the undocumented SessionIdInterface in PHP 5.5?

怎甘沉沦 提交于 2019-12-11 02:55:48

问题


In the PHP 5.5.1, there's a reference to a SessionIdInterface. However, it's still undocumented and only thing I could find was the interface definition:

interface SessionIdInterface  {
    public function create_sid ();
}
  • Is it safe to rely on this interface?
  • Where can I find some sort of documentation regarding this interface?

回答1:


SessionIdInterface was created as part of php-src pull request 109 and landed in PHP 5.5.1. Some details on it are discussed there.

In short, your SessionHandlerInterface implementation may also implement SessionIdInterface and provide a create_sid method that returns a string. PHP will automatically invoke create_sid instead of using the internal session creation functions as defined in php.ini.

Here's an example script that demonstrates the use.

<?php
class SillySessionHandler implements SessionHandlerInterface, SessionIdInterface {

    static $lol_sessions = [];

    public function open($save_path, $filename) { return true; }
    public function close() { return true; }
    public function destroy($session_id) { return true; }
    public function gc($lifetime) { return true; }
    public function read($session_id) { 
        return array_key_exists($session_id, static::$lol_sessions) ? static::$lol_sessions[$session_id] : null; 
    }
    public function write($session_id, $session_data) { 
        static::$lol_sessions[$session_id] = $session_data;
        echo "Session data: ", $session_data; 
    }

    public function create_sid() {
        $sid = bin2hex(openssl_random_pseudo_bytes(16));
        static::$lol_sessions[$sid] = [];
        return $sid;
    }

}

$handler = new SillySessionHandler;
session_set_save_handler($handler, true);
session_start();

echo "Your session ID is ", session_id(), "<hr>";
$_SESSION['foo'] = 'bar';

Example output:

Your session ID is 2e837e0c5f5ac1b23296d384a9aab2af


Session data: foo|s:3:"bar";



回答2:


According to PHP source code:

/* {{{ SessionIdInterface functions[]
*/
static const zend_function_entry php_session_id_iface_functions[] = {
        PHP_ABSTRACT_ME(SessionIdInterface, create_sid, arginfo_session_class_create_sid)
        { NULL, NULL, NULL }
};
/* }}} */

/* {{{ SessionHandler functions[]
 */
static const zend_function_entry php_session_class_functions[] = {
        PHP_ME(SessionHandler, open, arginfo_session_class_open, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, close, arginfo_session_class_close, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, read, arginfo_session_class_read, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, write, arginfo_session_class_write, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, destroy, arginfo_session_class_destroy, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, gc, arginfo_session_class_gc, ZEND_ACC_PUBLIC)
        PHP_ME(SessionHandler, create_sid, arginfo_session_class_create_sid, ZEND_ACC_PUBLIC)
        { NULL, NULL, NULL }
    };

There is no any other methods in SessionIdInterface and create_sid in this interface is the same with SessionHandler



来源:https://stackoverflow.com/questions/20910850/what-is-the-undocumented-sessionidinterface-in-php-5-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!