xref: /PHP-8.1/ext/session/tests/024.phpt (revision b5a14e6c)
1--TEST--
2session_set_save_handler test
3--EXTENSIONS--
4session
5--SKIPIF--
6<?php include('skipif.inc'); ?>
7--INI--
8session.use_cookies=0
9session.use_strict_mode=0
10session.cache_limiter=
11session.name=PHPSESSID
12session.serialize_handler=php
13--FILE--
14<?php
15error_reporting(E_ALL);
16ob_start();
17
18class handler {
19    public $data = 'baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:1;}}';
20
21    function open($save_path, $session_name): bool
22    {
23        print "OPEN: $session_name\n";
24        return true;
25    }
26    function close(): bool
27    {
28        return true;
29    }
30    function read($key): string|false
31    {
32        print "READ: $key\n";
33        return $GLOBALS["hnd"]->data;
34    }
35
36    function write($key, $val): bool
37    {
38        print "WRITE: $key, $val\n";
39        $GLOBALS["hnd"]->data = $val;
40        return true;
41    }
42
43    function destroy($key): bool
44    {
45        print "DESTROY: $key\n";
46        return true;
47    }
48
49    function gc() { return true; }
50}
51
52$hnd = new handler;
53
54class foo {
55    public $bar = "ok";
56    function method() { $this->yes++; }
57}
58
59session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
60
61session_id("test024");
62session_start();
63
64$baz = $_SESSION['baz'];
65$arr = $_SESSION['arr'];
66$baz->method();
67$arr[3]->method();
68
69var_dump($baz);
70var_dump($arr);
71
72session_write_close();
73
74session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
75session_start();
76
77var_dump($baz);
78var_dump($arr);
79
80session_destroy();
81?>
82--EXPECTF--
83OPEN: PHPSESSID
84READ: test024
85object(foo)#%d (2) {
86  ["bar"]=>
87  string(2) "ok"
88  ["yes"]=>
89  int(2)
90}
91array(1) {
92  [3]=>
93  object(foo)#%d (2) {
94    ["bar"]=>
95    string(2) "ok"
96    ["yes"]=>
97    int(2)
98  }
99}
100WRITE: test024, baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:2;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:2;}}
101OPEN: PHPSESSID
102READ: test024
103object(foo)#%d (2) {
104  ["bar"]=>
105  string(2) "ok"
106  ["yes"]=>
107  int(2)
108}
109array(1) {
110  [3]=>
111  object(foo)#%d (2) {
112    ["bar"]=>
113    string(2) "ok"
114    ["yes"]=>
115    int(2)
116  }
117}
118DESTROY: test024
119