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