1--TEST-- 2Test session_set_save_handler() : full handler implementation 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() : full handler implementation ***\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 (bool)file_put_contents($this->path . $id, $data); 38 } 39 40 public function destroy($id): bool { 41 @unlink($this->path . $id); 42 return true; 43 } 44 45 public function gc($maxlifetime): int|false { 46 foreach (glob($this->path . '*') as $filename) { 47 if (filemtime($filename) + $maxlifetime < time()) { 48 @unlink($filename); 49 } 50 } 51 return true; 52 } 53} 54 55$handler = new MySession2; 56session_set_save_handler(array($handler, 'open'), array($handler, 'close'), 57 array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc')); 58session_start(); 59 60$_SESSION['foo'] = "hello"; 61 62var_dump(session_id(), ini_get('session.save_handler'), $_SESSION); 63 64session_write_close(); 65session_unset(); 66 67session_start(); 68var_dump($_SESSION); 69 70session_write_close(); 71session_unset(); 72 73session_set_save_handler($handler); 74session_start(); 75 76$_SESSION['foo'] = "hello"; 77 78var_dump(session_id(), ini_get('session.save_handler'), $_SESSION); 79 80session_write_close(); 81session_unset(); 82 83session_start(); 84var_dump($_SESSION); 85 86session_write_close(); 87session_unset(); 88?> 89--EXPECTF-- 90*** Testing session_set_save_handler() : full handler implementation *** 91string(%d) "%s" 92string(4) "user" 93array(1) { 94 ["foo"]=> 95 string(5) "hello" 96} 97array(1) { 98 ["foo"]=> 99 string(5) "hello" 100} 101string(%d) "%s" 102string(4) "user" 103array(1) { 104 ["foo"]=> 105 string(5) "hello" 106} 107array(1) { 108 ["foo"]=> 109 string(5) "hello" 110} 111