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