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