summaryrefslogtreecommitdiff
path: root/ext/session/tests/session_set_save_handler_class_018.phpt
blob: dd1fe2129c8fe6b2a6d80d40f1acc709ba127c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
--TEST--
Test session_set_save_handler() function: class with validate_sid
--INI--
session.save_handler=files
session.name=PHPSESSID
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

ob_start();

echo "*** Testing session_set_save_handler() function: class with validate_sid ***\n";

class MySession2 extends SessionHandler {
    public $path;

    public function open($path, $name) {
        if (!$path) {
            $path = sys_get_temp_dir();
        }
        $this->path = $path . '/u_sess_' . $name;
        return true;
    }

    public function close() {
        return true;
    }

    public function read($id) {
        return (string)@file_get_contents($this->path . $id);
    }

    public function write($id, $data) {
        return file_put_contents($this->path . $id, $data)===FALSE ? FALSE : TRUE ;
    }

    public function destroy($id) {
        @unlink($this->path . $id);
    }

    public function gc($maxlifetime) {
        foreach (glob($this->path . '*') as $filename) {
            if (filemtime($filename) + $maxlifetime < time()) {
                @unlink($filename);
            }
        }
        return true;
    }

    public function create_sid() {
        return pathinfo(__FILE__)['filename'];
    }

    public function validate_sid($id) {
        return pathinfo(__FILE__)['filename']===$id;
    }
}

$handler = new MySession2;
session_set_save_handler($handler);
session_start();

$_SESSION['foo'] = "hello";

var_dump(session_id(), ini_get('session.save_handler'), $_SESSION);

session_write_close();
session_unset();

session_start();
var_dump($_SESSION);
--CLEAN--
<?php
@unlink(session_save_path().'/u_sess_PHPSESSIDsession_set_save_handler_class_018');
?>
--EXPECT--
*** Testing session_set_save_handler() function: class with validate_sid ***
string(34) "session_set_save_handler_class_018"
string(4) "user"
array(1) {
  ["foo"]=>
  string(5) "hello"
}
array(1) {
  ["foo"]=>
  string(5) "hello"
}