xref: /PHP-8.0/ext/session/tests/005.phpt (revision f8d79582)
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    function open($save_path, $session_name)
19    {
20        print "OPEN: $session_name\n";
21        return true;
22    }
23    function close()
24    {
25        print "CLOSE\n";
26        return true;
27    }
28    function read($key)
29    {
30        print "READ: $key\n";
31        return $GLOBALS["hnd"]->data;
32    }
33
34    function write($key, $val)
35    {
36        print "WRITE: $key, $val\n";
37        $GLOBALS["hnd"]->data = $val;
38        return true;
39    }
40
41    function destroy($key)
42    {
43        print "DESTROY: $key\n";
44        return true;
45    }
46
47    function gc() { return true; }
48}
49
50$hnd = new handler;
51
52class foo {
53    public $bar = "ok";
54    function method() { $this->yes++; }
55}
56
57session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
58
59session_id("abtest");
60session_start();
61session_decode($hnd->data);
62
63$_SESSION["baz"]->method();
64$_SESSION["arr"][3]->method();
65
66var_dump($_SESSION["baz"]);
67var_dump($_SESSION["arr"]);
68
69session_write_close();
70
71session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
72session_start();
73$_SESSION["baz"]->method();
74$_SESSION["arr"][3]->method();
75
76
77$_SESSION["c"] = 123;
78var_dump($_SESSION["baz"]);
79var_dump($_SESSION["arr"]);
80var_dump($_SESSION["c"]);
81
82session_write_close();
83
84session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
85session_start();
86var_dump($_SESSION["baz"]);
87var_dump($_SESSION["arr"]);
88var_dump($_SESSION["c"]);
89
90session_destroy();
91?>
92--EXPECT--
93OPEN: PHPSESSID
94READ: abtest
95object(foo)#4 (2) {
96  ["bar"]=>
97  string(2) "ok"
98  ["yes"]=>
99  int(2)
100}
101array(1) {
102  [3]=>
103  object(foo)#2 (2) {
104    ["bar"]=>
105    string(2) "ok"
106    ["yes"]=>
107    int(2)
108  }
109}
110WRITE: 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;}}
111CLOSE
112OPEN: PHPSESSID
113READ: abtest
114object(foo)#2 (2) {
115  ["bar"]=>
116  string(2) "ok"
117  ["yes"]=>
118  int(3)
119}
120array(1) {
121  [3]=>
122  object(foo)#4 (2) {
123    ["bar"]=>
124    string(2) "ok"
125    ["yes"]=>
126    int(3)
127  }
128}
129int(123)
130WRITE: 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;
131CLOSE
132OPEN: PHPSESSID
133READ: abtest
134object(foo)#4 (2) {
135  ["bar"]=>
136  string(2) "ok"
137  ["yes"]=>
138  int(3)
139}
140array(1) {
141  [3]=>
142  object(foo)#2 (2) {
143    ["bar"]=>
144    string(2) "ok"
145    ["yes"]=>
146    int(3)
147  }
148}
149int(123)
150DESTROY: abtest
151CLOSE
152