xref: /PHP-8.1/ext/session/tests/005.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    function open($save_path, $session_name): bool
21    {
22        print "OPEN: $session_name\n";
23        return true;
24    }
25    function close(): bool
26    {
27        print "CLOSE\n";
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("test005");
62session_start();
63session_decode($hnd->data);
64
65$_SESSION["baz"]->method();
66$_SESSION["arr"][3]->method();
67
68var_dump($_SESSION["baz"]);
69var_dump($_SESSION["arr"]);
70
71session_write_close();
72
73session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
74session_start();
75$_SESSION["baz"]->method();
76$_SESSION["arr"][3]->method();
77
78
79$_SESSION["c"] = 123;
80var_dump($_SESSION["baz"]);
81var_dump($_SESSION["arr"]);
82var_dump($_SESSION["c"]);
83
84session_write_close();
85
86session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
87session_start();
88var_dump($_SESSION["baz"]);
89var_dump($_SESSION["arr"]);
90var_dump($_SESSION["c"]);
91
92session_destroy();
93?>
94--EXPECT--
95OPEN: PHPSESSID
96READ: test005
97object(foo)#4 (2) {
98  ["bar"]=>
99  string(2) "ok"
100  ["yes"]=>
101  int(2)
102}
103array(1) {
104  [3]=>
105  object(foo)#2 (2) {
106    ["bar"]=>
107    string(2) "ok"
108    ["yes"]=>
109    int(2)
110  }
111}
112WRITE: test005, 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;}}
113CLOSE
114OPEN: PHPSESSID
115READ: test005
116object(foo)#2 (2) {
117  ["bar"]=>
118  string(2) "ok"
119  ["yes"]=>
120  int(3)
121}
122array(1) {
123  [3]=>
124  object(foo)#4 (2) {
125    ["bar"]=>
126    string(2) "ok"
127    ["yes"]=>
128    int(3)
129  }
130}
131int(123)
132WRITE: test005, 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;
133CLOSE
134OPEN: PHPSESSID
135READ: test005
136object(foo)#4 (2) {
137  ["bar"]=>
138  string(2) "ok"
139  ["yes"]=>
140  int(3)
141}
142array(1) {
143  [3]=>
144  object(foo)#2 (2) {
145    ["bar"]=>
146    string(2) "ok"
147    ["yes"]=>
148    int(3)
149  }
150}
151int(123)
152DESTROY: test005
153CLOSE
154