xref: /PHP-8.2/ext/session/tests/024.phpt (revision adb45a63)
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
21    function open($save_path, $session_name): bool
22    {
23        print "OPEN: $session_name\n";
24        return true;
25    }
26    function close(): bool
27    {
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("test024");
63session_start();
64
65$baz = $_SESSION['baz'];
66$arr = $_SESSION['arr'];
67$baz->method();
68$arr[3]->method();
69
70var_dump($baz);
71var_dump($arr);
72
73session_write_close();
74
75session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
76session_start();
77
78var_dump($baz);
79var_dump($arr);
80
81session_destroy();
82?>
83--EXPECTF--
84OPEN: PHPSESSID
85READ: test024
86object(foo)#%d (2) {
87  ["bar"]=>
88  string(2) "ok"
89  ["yes"]=>
90  int(2)
91}
92array(1) {
93  [3]=>
94  object(foo)#%d (2) {
95    ["bar"]=>
96    string(2) "ok"
97    ["yes"]=>
98    int(2)
99  }
100}
101WRITE: test024, 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;}}
102OPEN: PHPSESSID
103READ: test024
104object(foo)#%d (2) {
105  ["bar"]=>
106  string(2) "ok"
107  ["yes"]=>
108  int(2)
109}
110array(1) {
111  [3]=>
112  object(foo)#%d (2) {
113    ["bar"]=>
114    string(2) "ok"
115    ["yes"]=>
116    int(2)
117  }
118}
119DESTROY: test024
120