-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-mysql-session-handler.php
58 lines (46 loc) · 1.51 KB
/
php-mysql-session-handler.php
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
<?php
/*
Plugin Name: PHP Mysql Session Handler
Plugin URI: https://github.com/jsloyer/PHP-MySQL-Session-Handler
Description: This plugin will store php sessions in the database
Version: 1.0
Author: Jeff Sloyer
Author URI: https://github.com/jsloyer
License: Apache V2
*/
require_once("SessionHandler.php");
function sessionHandler() {
$session = new SessionHandler();
// add db data
$session->setDbDetails(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// OR alternatively send a MySQLi ressource
// $session->setDbConnection($mysqli);
$session->setDbTable("sessions");
session_set_save_handler(array($session, "open"),
array($session, "close"),
array($session, "read"),
array($session, "write"),
array($session, "destroy"),
array($session, "gc")
);
// The following prevents unexpected effects when using objects as save handlers.
register_shutdown_function("session_write_close");
session_start();
}
function createTableIfDoesNotExist() {
$query = "SELECT id FROM sessions";
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
$result = mysql_query($query);
if(empty($result)) {
$query = "CREATE TABLE sessions (
id varchar(255) NOT NULL,
data mediumtext NOT NULL,
timestamp int(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
$result = mysql_query($query);
}
}
add_filter("authenticate", "sessionHandler", 0);
register_activation_hook( __FILE__, "createTableIfDoesNotExist");