1--TEST--
2Test session_set_save_handler() : full handler implementation
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(SessionHandler $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() : full handler implementation ***\n";
20
21class MySession2 extends SessionHandler {
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		return true;
47	}
48
49	public function gc($maxlifetime) {
50		foreach (glob($this->path . '*') as $filename) {
51			if (filemtime($filename) + $maxlifetime < time()) {
52				@unlink($filename);
53			}
54		}
55		return true;
56	}
57}
58
59$handler = new MySession2;
60session_set_save_handler(array($handler, 'open'), array($handler, 'close'),
61	array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc'));
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
74session_write_close();
75session_unset();
76
77session_set_save_handler($handler);
78session_start();
79
80$_SESSION['foo'] = "hello";
81
82var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);
83
84session_write_close();
85session_unset();
86
87session_start();
88var_dump($_SESSION);
89
90session_write_close();
91session_unset();
92--EXPECTF--
93*** Testing session_set_save_handler() : full handler implementation ***
94string(%d) "%s"
95string(4) "user"
96array(1) {
97  ["foo"]=>
98  string(5) "hello"
99}
100array(1) {
101  ["foo"]=>
102  string(5) "hello"
103}
104string(%d) "%s"
105string(4) "user"
106array(1) {
107  ["foo"]=>
108  string(5) "hello"
109}
110array(1) {
111  ["foo"]=>
112  string(5) "hello"
113}
114