1--TEST--
2Test session_set_save_handler() function: interface
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: interface ***\n";
16
17class MySession2 implements SessionHandlerInterface {
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    }
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
54$handler = new MySession2;
55session_set_save_handler(array($handler, 'open'), array($handler, 'close'),
56    array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc'));
57session_start();
58
59$_SESSION['foo'] = "hello";
60
61var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);
62
63session_write_close();
64session_unset();
65
66session_start();
67var_dump($_SESSION);
68
69session_write_close();
70session_unset();
71
72session_set_save_handler($handler);
73session_start();
74
75$_SESSION['foo'] = "hello";
76
77var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);
78
79session_write_close();
80session_unset();
81
82session_start();
83var_dump($_SESSION);
84
85session_write_close();
86session_unset();
87?>
88--EXPECTF--
89*** Testing session_set_save_handler() function: interface ***
90string(%d) "%s"
91string(4) "user"
92array(1) {
93  ["foo"]=>
94  string(5) "hello"
95}
96array(1) {
97  ["foo"]=>
98  string(5) "hello"
99}
100string(%d) "%s"
101string(4) "user"
102array(1) {
103  ["foo"]=>
104  string(5) "hello"
105}
106array(1) {
107  ["foo"]=>
108  string(5) "hello"
109}
110