1--TEST-- 2Test session_set_save_handler() function: interface 3--EXTENSIONS-- 4session 5--FILE-- 6<?php 7 8ob_start(); 9 10echo "*** Testing session_set_save_handler() function: interface ***\n"; 11 12class MySession2 implements SessionHandlerInterface { 13 public $path; 14 15 public function open($path, $name): bool{ 16 if (!$path) { 17 $path = sys_get_temp_dir(); 18 } 19 $this->path = $path . '/u_sess_' . $name; 20 return true; 21 } 22 23 public function close(): bool { 24 return true; 25 } 26 27 public function read($id): string|false { 28 return (string)@file_get_contents($this->path . $id); 29 } 30 31 public function write($id, $data): bool { 32 return (bool)file_put_contents($this->path . $id, $data); 33 } 34 35 public function destroy($id): bool { 36 @unlink($this->path . $id); 37 } 38 39 public function gc($maxlifetime): int|false { 40 foreach (glob($this->path . '*') as $filename) { 41 if (filemtime($filename) + $maxlifetime < time()) { 42 @unlink($filename); 43 } 44 } 45 return true; 46 } 47} 48 49$handler = new MySession2; 50session_set_save_handler(array($handler, 'open'), array($handler, 'close'), 51 array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc')); 52session_start(); 53 54$_SESSION['foo'] = "hello"; 55 56var_dump(session_id(), ini_get('session.save_handler'), $_SESSION); 57 58session_write_close(); 59session_unset(); 60 61session_start(); 62var_dump($_SESSION); 63 64session_write_close(); 65session_unset(); 66 67session_set_save_handler($handler); 68session_start(); 69 70$_SESSION['foo'] = "hello"; 71 72var_dump(session_id(), ini_get('session.save_handler'), $_SESSION); 73 74session_write_close(); 75session_unset(); 76 77session_start(); 78var_dump($_SESSION); 79 80session_write_close(); 81session_unset(); 82?> 83--EXPECTF-- 84*** Testing session_set_save_handler() function: interface *** 85string(%d) "%s" 86string(4) "user" 87array(1) { 88 ["foo"]=> 89 string(5) "hello" 90} 91array(1) { 92 ["foo"]=> 93 string(5) "hello" 94} 95string(%d) "%s" 96string(4) "user" 97array(1) { 98 ["foo"]=> 99 string(5) "hello" 100} 101array(1) { 102 ["foo"]=> 103 string(5) "hello" 104} 105