(PHP 5 >= 5.4.0, PHP 7)
Examples:
Using SessionHandler to add encryption to internal PHP save handlers.
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
89
90
91
92
93
94
<?php
 
 /**
  * decrypt AES 256
  *
  * @param data $edata
  * @param string $password
  * @return decrypted data
  */
function decrypt($edata$password) {
    $data base64_decode($edata);
    $salt substr($data, 0, 16);
    $ct substr($data, 16);
 
    $rounds = 3; // depends on key length
    $data00 $password.$salt;
    $hash array();
    $hash[0] = hash('sha256'$data00, true);
    $result $hash[0];
    for ($i = 1; $i $rounds$i++) {
        $hash[$i] = hash('sha256'$hash[$i - 1].$data00, true);
        $result .= $hash[$i];
    }
    $key substr($result, 0, 32);
    $iv  substr($result, 32,16);
 
    return openssl_decrypt($ct'AES-256-CBC'$key, true, $iv);
  }
 
/**
 * crypt AES 256
 *
 * @param data $data
 * @param string $password
 * @return base64 encrypted data
 */
function encrypt($data$password) {
    // Set a random salt
    $salt = openssl_random_pseudo_bytes(16);
 
    $salted '';
    $dx '';
    // Salt the key(32) and iv(16) = 48
    while (strlen($salted) < 48) {
      $dx = hash('sha256'$dx.$password.$salt, true);
      $salted .= $dx;
    }
 
    $key substr($salted, 0, 32);
    $iv  substr($salted, 32,16);
 
    $encrypted_data = openssl_encrypt($data'AES-256-CBC'$key, true, $iv);
    return base64_encode($salt $encrypted_data);
}
 
class EncryptedSessionHandler extends SessionHandler
{
    private $key;
 
    public function __construct($key)
    {
        $this->key = $key;
    }
 
    public function read($id)
    {
        $data = parent::read($id);
 
        if (!$data) {
            return "";
        else {
            return decrypt($data$this->key);
        }
    }
 
    public function write($id$data)
    {
        $data = encrypt($data$this->key);
 
        return parent::write($id$data);
    }
}
 
// we'll intercept the native 'files' handler, but will equally work
// with other internal native handlers like 'sqlite', 'memcache' or 'memcached'
// which are provided by PHP extensions.
ini_set('session.save_handler''files');
 
$key 'secret_string';
$handler new EncryptedSessionHandler($key);
session_set_save_handler($handler, true);
session_start();
 
// proceed to set and retrieve values by key from $_SESSION
SessionHandler::destroy
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Destroy a session

2025-01-10 15:47:30
SessionHandler::write
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Write session data

2025-01-10 15:47:30
SessionHandler::gc
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Cleanup old sessions

2025-01-10 15:47:30
SessionHandler::read
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Read session data

2025-01-10 15:47:30
SessionHandler::open
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Initialize session

2025-01-10 15:47:30
SessionHandler::close
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.4.0, PHP 7) Close the session

2025-01-10 15:47:30
SessionHandler::create_sid
  • References/PHP/Function/Session/SessionHandler

(PHP 5 >= 5.5.1, PHP 7) Return a new session ID

2025-01-10 15:47:30