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