xref: /PHP-7.4/ext/session/tests/025.phpt (revision c58b3078)
1--TEST--
2custom save handler, multiple session_start()s, complex data structure test.
3--SKIPIF--
4<?php include('skipif.inc'); ?>
5--INI--
6session.use_cookies=0
7session.use_strict_mode=0
8session.cache_limiter=
9session.name=PHPSESSID
10session.serialize_handler=php
11--FILE--
12<?php
13error_reporting(E_ALL);
14ob_start();
15
16class handler {
17    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;}}';
18
19    function open($save_path, $session_name)
20    {
21        print "OPEN: $session_name\n";
22        return true;
23    }
24    function close()
25    {
26		print "CLOSE\n";
27        return true;
28    }
29    function read($key)
30    {
31        print "READ: $key\n";
32        return $GLOBALS["hnd"]->data;
33    }
34
35    function write($key, $val)
36    {
37        print "WRITE: $key, $val\n";
38        $GLOBALS["hnd"]->data = $val;
39        return true;
40    }
41
42    function destroy($key)
43    {
44        print "DESTROY: $key\n";
45        return true;
46    }
47
48    function gc() { return true; }
49}
50
51$hnd = new handler;
52
53class foo {
54    public $bar = "ok";
55    function method() { $this->yes++; }
56}
57
58session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
59
60session_id("abtest");
61session_start();
62$baz = $_SESSION['baz'];
63$arr = $_SESSION['arr'];
64$baz->method();
65$arr[3]->method();
66
67var_dump($baz);
68var_dump($arr);
69
70session_write_close();
71
72session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
73session_start();
74$baz = $_SESSION['baz'];
75$arr = $_SESSION['arr'];
76
77
78$baz->method();
79$arr[3]->method();
80
81
82$c = 123;
83$_SESSION['c'] = $c;
84var_dump($baz); var_dump($arr); var_dump($c);
85
86session_write_close();
87
88session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
89session_start();
90var_dump($baz); var_dump($arr); var_dump($c);
91
92session_destroy();
93?>
94--EXPECTF--
95OPEN: PHPSESSID
96READ: abtest
97object(foo)#%d (2) {
98  ["bar"]=>
99  string(2) "ok"
100  ["yes"]=>
101  int(2)
102}
103array(1) {
104  [3]=>
105  object(foo)#%d (2) {
106    ["bar"]=>
107    string(2) "ok"
108    ["yes"]=>
109    int(2)
110  }
111}
112WRITE: abtest, 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: abtest
116object(foo)#%d (2) {
117  ["bar"]=>
118  string(2) "ok"
119  ["yes"]=>
120  int(3)
121}
122array(1) {
123  [3]=>
124  object(foo)#%d (2) {
125    ["bar"]=>
126    string(2) "ok"
127    ["yes"]=>
128    int(3)
129  }
130}
131int(123)
132WRITE: abtest, 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: abtest
136object(foo)#%d (2) {
137  ["bar"]=>
138  string(2) "ok"
139  ["yes"]=>
140  int(3)
141}
142array(1) {
143  [3]=>
144  object(foo)#%d (2) {
145    ["bar"]=>
146    string(2) "ok"
147    ["yes"]=>
148    int(3)
149  }
150}
151int(123)
152DESTROY: abtest
153CLOSE
154