The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 02:13:15 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Perk's custom session handlers - using a database  (Read 1623 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: April 28, 2009, 07:00:10 PM »

I would'a sworn that I posted this a long time ago, but I couldn't find it. Who runs this friggin' place, anyway?

Anyhoo - if you post these functions into your code then they will overwrite the default session handlers and you can use a database instead. Note that these use my dbConnection class - I create an instance and store it in GLOBALS as $GLOBALS['utilDB'] so that any process that needs a quick handle to the database their already good to go.

Here's the code:
Code:
<?php

function _session_open($path$name) { return true; }
function 
_session_close() { return true; }
function 
_session_garbage_collection($maxlife) { return true; }
function 
_session_read($id) { return $GLOBALS['utilDB']->singleAnswer("select data from sessions where id='$id'"); }
function 
_session_destroy($id) { return $GLOBALS['utilDB']->query("delete from sessions where id='$id'"); }
function 
_session_write($id$data)
{
if (!$GLOBALS['inhibitSessionWrite'])
{
$data mysql_escape_string($data);
$now date('Y-m-d H:i:s'time());
$duration = (time() - $_SESSION['starttime']) + 15// Add 15 seconds for the last page they're on...
$sql = <<<SQL
replace into sessions
(id, data, siteid, firsttouch, lasttouch, duration, pagecount) values
('
$id', '$data', {$GLOBALS['siteID']}, '{$_SESSION['firsttouch']}', '$now', $duration{$_SESSION['pagecount']})
SQL;
$GLOBALS['utilDB']->query($sql);
}
return true;
}

?>


Here's the create SQL needed for the table used above:

CREATE TABLE sessions (
  id varchar(64) NOT NULL,
  siteid int(11) NOT NULL,
  firsttouch datetime NOT NULL,
  lasttouch datetime NOT NULL,
  duration int(11) NOT NULL,
  pagecount int(11) NOT NULL,
  `data` text NOT NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


I then use a cron job once a minute to watch for sessions where the lasttouch is outside of my threshold for life - then I distill it into an aggregate table so that I keep the stats but let go of the session record.

Enjoy!
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 28, 2009, 07:01:23 PM »

Looking back at it, I also employ the global flag, inhibitSessionWrite - that is my own variable for if (some process) doesn't want the session stored (this time around).
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: April 30, 2009, 10:28:08 AM »

Sweet meat dude.

Thanks.
Logged

hai
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!