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