找回密码
 新建账号

PHP SessionUpdateTimestampHandlerInterface validateId updateTimestamp

[复制链接]
php 发表于 2017/12/17 00:50 | 显示全部楼层 |阅读模式
PHP Session session_set_save_handler()的OOP面向对象原型不能支持validate_sid()和update_timestamp()两个方法,如果需要像session_set_save_handler()的非面向对象原型一样支持验证session id和更新指定session的时间戳,需要同时implements SessionHandlerInterface和SessionUpdateTimestampHandlerInterface两个接口,其中SessionHandlerInterface是PHP 5.4引进的,SessionUpdateTimestampHandlerInterface是PHP 7.0引进的,并且SessionUpdateTimestampHandlerInterface目前在PHP手册文档中找不到任何说明。
SessionUpdateTimestampHandlerInterface有两个抽象方法,分别是SessionUpdateTimestampHandlerInterface::validateId()和SessionUpdateTimestampHandlerInterface::updateTimestamp(),其中SessionUpdateTimestampHandlerInterface::validateId()参数为session id, 用来检测传入的session id是否有效,返回值为布尔值,如果为true表示session id有效,如果为false,表示无效,此时PHP会自动重新生成一个新的session id,SessionUpdateTimestampHandlerInterface::updateTimestamp()第一个参数为session id,第二个参数为session data,用来更新指定session数据的时间戳,当启用了PHP 7.0开始支持的session.lazy_write以后, 如果在脚本运行周期结束后SESSION数据没有发生变化,PHP不会重新写入SESSOIN数据,在这种情况下,SessionUpdateTimestampHandlerInterface::updateTimestamp()可以用来更改指定session的数据,并不限于字面意义上的时间戳。
以下代码为一个类结构文件,每个方法具体代码已省略。
  1. <?php
  2.         /*
  3.            @author Wu Xiancheng from wuxiancheng.cn
  4.            Code structure for PHP 7.0+ only because SessionUpdateTimestampHandlerInterface is introduced in PHP 7.0
  5.            With this class you can validate php session id and update the timestamp of php session data
  6.            with the OOP prototype of session_set_save_handler() in PHP 7.0+
  7.         */
  8.         class PHPSessionXHandler implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface {
  9.                 public function close(){
  10.                         // return value should be true for success or false for failure
  11.                         // ...
  12.                 }
  13.                 public function destroy($sessionId){
  14.                         // return value should be true for success or false for failure
  15.                         // ...
  16.                 }
  17.                 public function gc($maximumLifetime){
  18.                         // return value should be true for success or false for failure
  19.                         // ...
  20.                 }
  21.                 public function open($sessionSavePath, $sessionName){
  22.                         // return value should be true for success or false for failure
  23.                         // ...
  24.                 }
  25.                 public function read($sessionId){
  26.                         // return value should be the session data or an empty string
  27.                         // ...
  28.                 }
  29.                 public function write($sessionId, $sessionData){
  30.                         // return value should be true for success or false for failure
  31.                         // ...
  32.                 }
  33.                 public function create_sid(){
  34.                         // available since PHP 5.5.1
  35.                         // invoked internally when a new session id is needed
  36.                         // no parameter is needed and return value should be the new session id created
  37.                         // ...
  38.                 }
  39.                 public function validateId($sessionId){
  40.                         // implements SessionUpdateTimestampHandlerInterface::validateId()
  41.                         // available since PHP 7.0
  42.                         // return value should be true if the session id is valid otherwise false
  43.                         // if false is returned a new session id will be generated by php internally
  44.                         // ...
  45.                 }
  46.                 public function updateTimestamp($sessionId, $sessionData){
  47.                         // implements SessionUpdateTimestampHandlerInterface::validateId()
  48.                         // available since PHP 7.0
  49.                         // return value should be true for success or false for failure
  50.                         // ...
  51.                 }
  52.         }
  53. ?>
复制代码

手机版|轻松E站

GMT+8, 2024/3/19 11:51

快速回复 返回顶部 返回列表