xref: /PHP-8.1/ext/session/tests/004.phpt (revision b5a14e6c)
1--TEST--
2session_set_save_handler 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        return true;
28    }
29    function read($key): string|false
30    {
31        print "READ: $key\n";
32        return $GLOBALS["hnd"]->data;
33    }
34
35    function write($key, $val): bool
36    {
37        print "WRITE: $key, $val\n";
38        $GLOBALS["hnd"]->data = $val;
39        return true;
40    }
41
42    function destroy($key): bool
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("test004");
61session_start();
62$_SESSION["baz"]->method();
63$_SESSION["arr"][3]->method();
64
65var_dump($_SESSION["baz"]);
66var_dump($_SESSION["arr"]);
67
68session_write_close();
69
70session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
71session_start();
72
73var_dump($_SESSION["baz"]);
74var_dump($_SESSION["arr"]);
75
76session_destroy();
77?>
78--EXPECT--
79OPEN: PHPSESSID
80READ: test004
81object(foo)#2 (2) {
82  ["bar"]=>
83  string(2) "ok"
84  ["yes"]=>
85  int(2)
86}
87array(1) {
88  [3]=>
89  object(foo)#3 (2) {
90    ["bar"]=>
91    string(2) "ok"
92    ["yes"]=>
93    int(2)
94  }
95}
96WRITE: test004, 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;}}
97OPEN: PHPSESSID
98READ: test004
99object(foo)#3 (2) {
100  ["bar"]=>
101  string(2) "ok"
102  ["yes"]=>
103  int(2)
104}
105array(1) {
106  [3]=>
107  object(foo)#2 (2) {
108    ["bar"]=>
109    string(2) "ok"
110    ["yes"]=>
111    int(2)
112  }
113}
114DESTROY: test004
115