xref: /PHP-7.2/ext/session/tests/004.phpt (revision 7f196e32)
1--TEST--
2session_set_save_handler 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        return true;
26    }
27    function read($key)
28    {
29        print "READ: $key\n";
30        return $GLOBALS["hnd"]->data;
31    }
32
33    function write($key, $val)
34    {
35        print "WRITE: $key, $val\n";
36		$GLOBALS["hnd"]->data = $val;
37        return true;
38    }
39
40    function destroy($key)
41    {
42        print "DESTROY: $key\n";
43        return true;
44    }
45
46    function gc() { return true; }
47}
48
49$hnd = new handler;
50
51class foo {
52    public $bar = "ok";
53    function method() { $this->yes++; }
54}
55
56session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
57
58session_id("abtest");
59session_start();
60$_SESSION["baz"]->method();
61$_SESSION["arr"][3]->method();
62
63var_dump($_SESSION["baz"]);
64var_dump($_SESSION["arr"]);
65
66session_write_close();
67
68session_set_save_handler(array($hnd, "open"), array($hnd, "close"), array($hnd, "read"), array($hnd, "write"), array($hnd, "destroy"), array($hnd, "gc"));
69session_start();
70
71var_dump($_SESSION["baz"]);
72var_dump($_SESSION["arr"]);
73
74session_destroy();
75?>
76--EXPECT--
77OPEN: PHPSESSID
78READ: abtest
79object(foo)#2 (2) {
80  ["bar"]=>
81  string(2) "ok"
82  ["yes"]=>
83  int(2)
84}
85array(1) {
86  [3]=>
87  object(foo)#3 (2) {
88    ["bar"]=>
89    string(2) "ok"
90    ["yes"]=>
91    int(2)
92  }
93}
94WRITE: 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;}}
95OPEN: PHPSESSID
96READ: abtest
97object(foo)#3 (2) {
98  ["bar"]=>
99  string(2) "ok"
100  ["yes"]=>
101  int(2)
102}
103array(1) {
104  [3]=>
105  object(foo)#2 (2) {
106    ["bar"]=>
107    string(2) "ok"
108    ["yes"]=>
109    int(2)
110  }
111}
112DESTROY: abtest
113