MySQL Sessionhandler
Dit is een script waarmee je zelf je sessies kan onderhouden. Wat hier nu he voordeel van is: 1. Het is veiliger, als je een sharedhost gebruikt staan alle bestanden van alle klanten in dezelfde folder (/tmp). Makkelijk dus om dan een sessie over te nemen. 2. De mogelijkheid een sessie terug te kunnen geven aan een gebruiker. Ook is er de mogelijkheid om te kijken welke gebruikers online zijn door een extra kolom aan te maken en de session tabel uit te lezen. *Om je script wat te optimaliseren kun je het beste nadat je alle sessies hebt gemaakt/geupdate de functie session_write_close() aanroepen. Deze zorgt er voor dat de sessie wordt opgeslagen en de verbinding met de database gesloten. Voor extra informatie: http://www.php.net/session_set_save_handler http://www.php.net/session_write_close http://nl.php.net/sessions Ik sta open voor commentaar en suggesties. er komt ook een pgSQL versie.
session.php
<?php
/* Title: Session handler
* Author: Jurgen Meijer
* E-mail: [email protected]
*
* Date: 19-04-2008
* License: GNU/GPL
*
* PHP version: 4+
* MySQL version 4+
*
* This is a session handler written for PHP5 but may work in PHP4 aswell. This version is a mysql only version.
* This handler is only tested in PHP5 and MySQL 5.
*
* Note: session.auto_start should be turned off in the php.ini file to allow custom sessionhandling.
*
* SQL:
*
CREATE TABLE session (
session_id VARCHAR( 32 ) NOT NULL ,
session_data MEDIUMTEXT NOT NULL ,
session_garbage TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL ,
PRIMARY KEY (session_id)
) ENGINE = innodb;
*/
function open_session()
{
//Global variable to be used within all the functions.
global $db;
//Connect to the database.
if($db = mysql_connect('localhost', 'root',''))
{
//if succeeded select the database.
mysql_select_db('sessionhandler');
return true;
}
else
{
return false;
}
}
function close_session()
{
//Global variable to be used within all the functions.
global $db;
//Close the connection to the database server by setting the $db variable to NULL.
$db = NULL;
return true;
}
function read_session($session_id)
{
//Global variable to be used within all the functions.
global $db;
//Selecting the data from the database where the current session_id equals the one in the database.
$query = "SELECT
session_data
FROM
session
WHERE
session_id='".$session_id."' ";
//check if the query succeedes.
if($result = mysql_query($query))
{
//If a row meets the requirement list the data and return it.
if(mysql_num_rows($result) == 1)
{
list($data) = mysql_fetch_array($result, MYSQL_NUM);
return $data;
}
else
{
//If there is no result, send back an empty string.
return '';
}
}
else
{
//If the query fails, send back an empty string.
return '';
}
}
function write_session($session_id, $session_data)
{
//Global variable to be used within all the functions.
global $db;
//Replace the session_data with the new data.
//* Replace is a SQL function that works as an INSERT/UPDATE.
//If there is no row, create a new one, else update the current.
$query = " REPLACE INTO
session
(
session_id,
session_data,
session_garbage
)
VALUES
(
'".$session_id."' ,
'".$session_data."',
CURRENT_TIMESTAMP)";
if(mysql_query($query))
{
//if the query succeedes return the affected rows (should allways be 1).
return mysql_affected_rows();
}
else
{
return false;
}
}
function destroy_session($session_id)
{
//Global variable to be used within all the functions.
global $db;
//Delete our session, this function will be called when we use session_destroy().
$query = "DELETE FROM
session
WHERE
session_id='".$session_id."'";
//If the query succeedes empty the $_SESSION array and return the affected rows.
if(mysql_query($query))
{
$_SESSION = array();
return mysql_affected_rows();
}
else
{
return false;
}
}
function clean_session($seconds)
{
//Global variable to be used within all the functions.
global $db;
//Delete items older than the amount of seconds wich is defined in the php.ini file.
//Default is after the browser is closed(0 seconds).
$query = "DELETE FROM
session
WHERE
DATE_ADD(session_garbage, INTERVAL ".$seconds." SECOND) < NOW()";
//Return the affected rows when the query succeedes.
if(mysql_query($query))
{
return mysql_affected_rows();
}
else
{
return false;
}
}
//Set our brand new sessionhandler to be used by PHP.
session_set_save_handler('open_session','close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');
//Start the session as you would normally do.
session_start();
?>
En het aanroepen van het script:
<?php
require 'session.php';
$_SESSION['blaat'] = 'LOL';
$_SESSION['schaap'] = 1;
session_write_close();
?>
Reacties
0