xref: /PHP-8.2/ext/session/tests/025.phpt (revision adb45a63)
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    public $yes;
58    function method() { $this->yes++; }
59}
60
61session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
62
63session_id("test025");
64session_start();
65$baz = $_SESSION['baz'];
66$arr = $_SESSION['arr'];
67$baz->method();
68$arr[3]->method();
69
70var_dump($baz);
71var_dump($arr);
72
73session_write_close();
74
75session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
76session_start();
77$baz = $_SESSION['baz'];
78$arr = $_SESSION['arr'];
79
80
81$baz->method();
82$arr[3]->method();
83
84
85$c = 123;
86$_SESSION['c'] = $c;
87var_dump($baz); var_dump($arr); var_dump($c);
88
89session_write_close();
90
91session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
92session_start();
93var_dump($baz); var_dump($arr); var_dump($c);
94
95session_destroy();
96?>
97--EXPECTF--
98OPEN: PHPSESSID
99READ: test025
100object(foo)#%d (2) {
101  ["bar"]=>
102  string(2) "ok"
103  ["yes"]=>
104  int(2)
105}
106array(1) {
107  [3]=>
108  object(foo)#%d (2) {
109    ["bar"]=>
110    string(2) "ok"
111    ["yes"]=>
112    int(2)
113  }
114}
115WRITE: 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;}}
116CLOSE
117OPEN: PHPSESSID
118READ: test025
119object(foo)#%d (2) {
120  ["bar"]=>
121  string(2) "ok"
122  ["yes"]=>
123  int(3)
124}
125array(1) {
126  [3]=>
127  object(foo)#%d (2) {
128    ["bar"]=>
129    string(2) "ok"
130    ["yes"]=>
131    int(3)
132  }
133}
134int(123)
135WRITE: 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;
136CLOSE
137OPEN: PHPSESSID
138READ: test025
139object(foo)#%d (2) {
140  ["bar"]=>
141  string(2) "ok"
142  ["yes"]=>
143  int(3)
144}
145array(1) {
146  [3]=>
147  object(foo)#%d (2) {
148    ["bar"]=>
149    string(2) "ok"
150    ["yes"]=>
151    int(3)
152  }
153}
154int(123)
155DESTROY: test025
156CLOSE
157