xref: /PHP-8.2/ext/session/tests/004.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    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    public $yes;
56    function method() { $this->yes++; }
57}
58
59session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
60
61session_id("test004");
62session_start();
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
74var_dump($_SESSION["baz"]);
75var_dump($_SESSION["arr"]);
76
77session_destroy();
78?>
79--EXPECT--
80OPEN: PHPSESSID
81READ: test004
82object(foo)#2 (2) {
83  ["bar"]=>
84  string(2) "ok"
85  ["yes"]=>
86  int(2)
87}
88array(1) {
89  [3]=>
90  object(foo)#3 (2) {
91    ["bar"]=>
92    string(2) "ok"
93    ["yes"]=>
94    int(2)
95  }
96}
97WRITE: 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;}}
98OPEN: PHPSESSID
99READ: test004
100object(foo)#3 (2) {
101  ["bar"]=>
102  string(2) "ok"
103  ["yes"]=>
104  int(2)
105}
106array(1) {
107  [3]=>
108  object(foo)#2 (2) {
109    ["bar"]=>
110    string(2) "ok"
111    ["yes"]=>
112    int(2)
113  }
114}
115DESTROY: test004
116