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