xref: /PHP-8.1/ext/session/tests/025.phpt (revision b5a14e6c)
1--TEST--
2custom save handler, multiple session_start()s, complex data structure 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        print "CLOSE\n";
29        return true;
30    }
31    function read($key): string|false
32    {
33        print "READ: $key\n";
34        return $GLOBALS["hnd"]->data;
35    }
36
37    function write($key, $val): bool
38    {
39        print "WRITE: $key, $val\n";
40        $GLOBALS["hnd"]->data = $val;
41        return true;
42    }
43
44    function destroy($key): bool
45    {
46        print "DESTROY: $key\n";
47        return true;
48    }
49
50    function gc() { return true; }
51}
52
53$hnd = new handler;
54
55class foo {
56    public $bar = "ok";
57    function method() { $this->yes++; }
58}
59
60session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
61
62session_id("test025");
63session_start();
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$baz = $_SESSION['baz'];
77$arr = $_SESSION['arr'];
78
79
80$baz->method();
81$arr[3]->method();
82
83
84$c = 123;
85$_SESSION['c'] = $c;
86var_dump($baz); var_dump($arr); var_dump($c);
87
88session_write_close();
89
90session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
91session_start();
92var_dump($baz); var_dump($arr); var_dump($c);
93
94session_destroy();
95?>
96--EXPECTF--
97OPEN: PHPSESSID
98READ: test025
99object(foo)#%d (2) {
100  ["bar"]=>
101  string(2) "ok"
102  ["yes"]=>
103  int(2)
104}
105array(1) {
106  [3]=>
107  object(foo)#%d (2) {
108    ["bar"]=>
109    string(2) "ok"
110    ["yes"]=>
111    int(2)
112  }
113}
114WRITE: test025, 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;}}
115CLOSE
116OPEN: PHPSESSID
117READ: test025
118object(foo)#%d (2) {
119  ["bar"]=>
120  string(2) "ok"
121  ["yes"]=>
122  int(3)
123}
124array(1) {
125  [3]=>
126  object(foo)#%d (2) {
127    ["bar"]=>
128    string(2) "ok"
129    ["yes"]=>
130    int(3)
131  }
132}
133int(123)
134WRITE: test025, baz|O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:3;}arr|a:1:{i:3;O:3:"foo":2:{s:3:"bar";s:2:"ok";s:3:"yes";i:3;}}c|i:123;
135CLOSE
136OPEN: PHPSESSID
137READ: test025
138object(foo)#%d (2) {
139  ["bar"]=>
140  string(2) "ok"
141  ["yes"]=>
142  int(3)
143}
144array(1) {
145  [3]=>
146  object(foo)#%d (2) {
147    ["bar"]=>
148    string(2) "ok"
149    ["yes"]=>
150    int(3)
151  }
152}
153int(123)
154DESTROY: test025
155CLOSE
156