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