View difference between Paste ID: K8421m7U and tzL47q6F
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
class MySqliSessionHandler implements SessionHandlerInterface
4
{
5
    private $mysqli;
6
    private $sessionName;
7
8
    /**
9
     * MySessionHandler constructor.
10
     * @param $mysqli
11
     */
12
    public function __construct(mysqli $mysqli)
13
    {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    public function open($savePath, $sessionName)
18
    {
19
20
        $this->sessionName = $sessionName;
21
return true;
22
    }
23
24
25
    public function close()
26
    {
27
        return true;
28
    }
29
30
    public function read($id)
31
    {
32
        $sql =sprintf("SELECT value FROM session WHERE id = '%s'",$this->mysqli->escape_string($id));
33
        $statement = $this->mysqli->query($sql);
34
        return $statement->fetch_assoc()['value'];
35
    }
36
37
    public function write($id, $data)
38
    {
39
        $sql =sprintf("INSERT INTO session (id,value,created) VALUES('%s','%s',NOW())",$this->mysqli->escape_string($id),$this->mysqli->escape_string($data));
40
41
        $this->mysqli->query($sql);
42
        return $this->mysqli->affected_rows > 0;
43
44
    }
45
46
    public function destroy($id)
47
    {
48
        $sql =sprintf("DELETE FROM session WHERE id='%s'",$this->mysqli->escape_string($id));
49
50
        $this->mysqli->query($sql);
51
        return $this->mysqli->affected_rows > 0;
52
    }
53
54
    public function gc($maxlifetime)
55
    {
56
        $sql ="DELETE FROM session WHERE created +".$maxlifetime." < NOW()";
57
58
        $this->mysqli->query($sql);
59
        return $this->mysqli->affected_rows > 0;
60
    }
61
}