xref: /PHP-8.2/ext/session/tests/gh7787.phpt (revision 0db03c41)
1--TEST--
2GH-7787: Provide more SessionHandler failure information
3--EXTENSIONS--
4session
5--SKIPIF--
6<?php include('skipif.inc'); ?>
7--INI--
8session.use_strict_mode=0
9session.save_handler=files
10--FILE--
11<?php
12class MySessionHandler extends SessionHandler implements SessionUpdateTimestampHandlerInterface
13{
14    public function open($path, $sessname): bool {
15        return true;
16    }
17
18    public function close(): bool {
19        return true;
20    }
21
22    public function read($sessid): string|false {
23        return 'foo|s:3:"foo";';
24    }
25
26    public function write($sessid, $sessdata): bool {
27        return false;
28    }
29
30    public function destroy($sessid): bool {
31        return true;
32    }
33
34    public function gc($maxlifetime): int|false {
35        return true;
36    }
37
38    public function create_sid(): string {
39        return sha1(random_bytes(32));
40    }
41
42    public function validateId($sid): bool {
43        return true;
44    }
45
46    public function updateTimestamp($sessid, $sessdata): bool {
47        return false;
48    }
49}
50
51ob_start();
52
53$handler = new MySessionHandler();
54session_set_save_handler($handler);
55
56session_start();
57$_SESSION['foo'] = 'bar';
58session_write_close();
59
60session_start();
61session_write_close();
62
63session_set_save_handler(
64    fn() => true,
65    fn() => true,
66    fn() => 'foo|s:3:"foo";',
67    fn() => false,
68    fn() => true,
69    fn() => true,
70    fn() => sha1(random_bytes(32)),
71    fn() => true,
72    fn() => false,
73);
74
75session_start();
76$_SESSION['foo'] = 'bar';
77session_write_close();
78
79session_start();
80session_write_close();
81
82?>
83--EXPECTF--
84Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: MySessionHandler::write) in %s on line %d
85
86Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: MySessionHandler::updateTimestamp) in %s on line %d
87
88Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: write) in %s on line %d
89
90Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: update_timestamp) in %s on line %d
91