database backed sessions

This commit is contained in:
Andrew Dolgov 2006-03-02 09:10:43 +01:00
parent 36ae0574dd
commit 36bfab86c2
15 changed files with 137 additions and 13 deletions

View file

@ -1,6 +1,6 @@
<?
session_start();
require_once "sessions.php";
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: -1");

View file

@ -3,8 +3,8 @@
require_once "db.php";
if (! DISABLE_SESSIONS) {
session_start();
require_once "sessions.php";
if (!$_SESSION["prefs_cache"])
$_SESSION["prefs_cache"] = array();
}

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "sanity_check.php";
require_once "version.php";

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "config.php";
require_once "functions.php";

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "sanity_check.php";
require_once "functions.php";

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "sanity_check.php";
require_once "version.php";

View file

@ -16,6 +16,7 @@ drop table if exists ttrss_feeds;
drop table if exists ttrss_feed_categories;
drop table if exists ttrss_users;
drop table if exists ttrss_themes;
drop table if exists ttrss_sessions;
begin;
@ -255,4 +256,11 @@ create table ttrss_scheduled_updates (id integer not null primary key auto_incre
foreign key (owner_uid) references ttrss_users(id) ON DELETE CASCADE,
foreign key (feed_id) references ttrss_feeds(id) ON DELETE CASCADE) TYPE=InnoDB;
create table ttrss_sessions (int_id integer not null primary key auto_increment,
id varchar(300) unique not null,
data text,
expire integer not null,
index (id),
index (expire)) TYPE=InnoDB;
commit;

View file

@ -16,6 +16,7 @@ drop table ttrss_feeds;
drop table ttrss_feed_categories;
drop table ttrss_users;
drop table ttrss_themes;
drop table ttrss_sessions;
begin;
@ -230,4 +231,12 @@ create table ttrss_scheduled_updates (id serial not null primary key,
feed_id integer default null references ttrss_feeds(id) ON DELETE CASCADE,
entered timestamp not null default NOW());
create table ttrss_sessions (int_id serial not null primary key,
id varchar(300) unique not null,
data text,
expire integer not null);
create index ttrss_sessions_id_index on ttrss_sessions(id);
create index ttrss_sessions_expire_index on ttrss_sessions(expire);
commit;

View file

@ -5,5 +5,12 @@ update ttrss_entries set author = '';
alter table ttrss_entries change author author varchar(250) not null;
alter table ttrss_entries alter column author set default '';
create table ttrss_sessions (int_id integer not null primary key auto_increment,
id varchar(300) unique not null,
data text,
expire integer not null,
index (id),
index (expire)) TYPE=InnoDB;
update ttrss_version set schema_version = 6;

View file

@ -7,6 +7,14 @@ update ttrss_entries set author = '';
alter table ttrss_entries alter column author set not null;
alter table ttrss_entries alter column author set default '';
create table ttrss_sessions (int_id serial not null primary key,
id varchar(300) unique not null,
data text,
expire integer not null);
create index ttrss_sessions_id_index on ttrss_sessions(id);
create index ttrss_sessions_expire_index on ttrss_sessions(expire);
update ttrss_version set schema_version = 6;
commit;

92
sessions.php Normal file
View file

@ -0,0 +1,92 @@
<?
// Original from http://www.daniweb.com/code/snippet43.html
require_once "config.php";
require_once "db.php";
$session_expire = 600;
ini_set("session.gc_probability", 50);
function open ($s, $n) {
global $session_connection;
$session_connection = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
return true;
}
function read ($id){
global $session_connection,$session_read;
$query = "SELECT data FROM ttrss_sessions WHERE id='$id'";
$res = db_query($session_connection, $query);
if (db_num_rows($res) != 1) {
return "";
} else {
$session_read = db_fetch_assoc($res);
$session_read["data"] = base64_decode($session_read["data"]);
return $session_read["data"];
}
}
function write ($id, $data) {
if (! $data) {
return false;
}
global $session_connection, $session_read, $session_expire;
$expire = time() + $session_expire;
$data = db_escape_string(base64_encode($data), $session_connection);
if ($session_read) {
$query = "UPDATE ttrss_sessions SET data='$data',
expire='$expire' WHERE id='$id'";
} else {
$query = "INSERT INTO ttrss_sessions (id, data, expire)
VALUES ('$id', '$data', '$expire')";
}
db_query($session_connection, $query);
return true;
}
function close () {
global $session_connection;
db_close($session_connection);
return true;
}
function destroy ($id) {
global $session_connection;
$query = "DELETE FROM ttrss_sessions WHERE id = '$id'";
db_query($session_connection, $query);
return true;
}
function gc ($expire) {
global $session_connection;
$query = "DELETE FROM ttrss_sessions WHERE expire < " . time();
db_query($session_connection, $query);
}
session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");
session_start();
?>

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "sanity_check.php";
require_once "version.php";

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
require_once "sanity_check.php";
require_once "version.php";

View file

@ -1,5 +1,5 @@
<?
session_start();
require_once "sessions.php";
define('MAX_SCHEMA_VERSION', 5);

View file

@ -1,6 +1,6 @@
<?
session_start();
require_once "sessions.php";
require_once "config.php";
require_once "functions.php";
require_once "db.php";