note 96305 modified in function.session-set-save-handler by danbrown
| From: | danbrown@php.net | Date: | Fri, 19 Feb 2010 17:18:11 +0000 |
| Subject: | note 96305 modified in function.session-set-save-handler by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166179@lists.php.net to get a copy of this message | ||
I have been done with session problems creating a new and my own session system.
This is very simple, bellow the class file, a good pratice is include these file as your first line:
<?php
class SessionDB {
private $data=null;
private $session_id=null;
private $minutes_to_expire=3600; // TIME TO MAINTAIN DATA ON DB
public function __construct(){
global $SESSION;
if (isset($_COOKIE['session_id'])){
$this->session_id = $_COOKIE['session_id'];
} else {
$this->session_id = md5(microtime().rand(1,9999999999999999999999999)); // GENERATE A
RANDOM ID
setcookie('session_id',$this->session_id);
$sql = "INSERT INTO
tb_session_db (session_id,
updated_on) VALUES ('{$this->session_id}', NOW())";
mysql_query($sql);
}
$sql = "SELECT value FROM tb_session_db WHERE
session_id='{$this->session_id}'";
$query = mysql_query($sql);
$this->data = unserialize(mysql_result($query, 0, 'value'));
$SESSION = $this->data;
}
private function expire(){
$date_to_delete = date("Y-m-d H:i:s", time()-60*$this->minutes_to_expire);
$sql = "DELETE FROM tb_session_db WHERE update_on <=
'$date_to_delete'";
mysql_query($sql);
}
public function __destruct(){
global $SESSION;
$this->data = serialize($SESSION);
$sql = "UPDATE tb_session_db SET
value='{$this->data}', updated_on=NOW() WHERE
session_id='{$this->session_id}'";
mysql_query($sql);
$this->expire();
}
}
/*
TABLE STRUCTURE
CREATE TABLE IF NOT EXISTS tb_session_db (
session_id varchar(32) NOT NULL,
value blob,
updated_on datetime DEFAULT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/
?>
With this file included, connected to MySQL database who has the table tb_session_db I
can easy do that into my config file:
<?php
require_once('session_db.php');
define('MYSQL_HOST','mysql.example.com');
define('MYSQL_USER','.......');
define('MYSQL_PASS','.......');
define('MYSQL_BASE','.......');
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Cannot connect');
mysql_select_db(MYSQL_BASE) or die('Cannot select DB');
mysql_set_charset('utf8_general_ci');
$SESSION = null;
global $SESSION;
$session_db = new SessionDB();
?>
To use $SESSION on a function/class method, you must call:
global $SESSION
For who will not work with this over classes or functions, it is a very simple/fast way to work!
--was--
I have been done with session problems creating a new and my own session system.
This is very simple, bellow the class file, a good pratice is include these file as your first line:
<?
class SessionDB {
private $data=null;
private $session_id=null;
private $minutes_to_expire=3600; // TIME TO MAINTAIN DATA ON DB
public function __construct(){
global $SESSION;
if (isset($_COOKIE['session_id'])){
$this->session_id = $_COOKIE['session_id'];
} else {
$this->session_id = md5(microtime().rand(1,9999999999999999999999999)); // GENERATE A
RANDOM ID
setcookie('session_id',$this->session_id);
$sql = "INSERT INTO tb_session_db (session_id,
updated_on) VALUES ('{$this->session_id}', NOW())";
mysql_query($sql);
}
$sql = "SELECT value FROM tb_session_db WHERE
session_id='{$this->session_id}'";
$query = mysql_query($sql);
$this->data = unserialize(mysql_result($query, 0, 'value'));
$SESSION = $this->data;
}
private function expire(){
$date_to_delete = date("Y-m-d H:i:s", time()-60*$this->minutes_to_expire);
$sql = "DELETE FROM tb_session_db WHERE update_on <=
'$date_to_delete'";
mysql_query($sql);
}
public function __destruct(){
global $SESSION;
$this->data = serialize($SESSION);
$sql = "UPDATE tb_session_db SET
value='{$this->data}', updated_on=NOW() WHERE
session_id='{$this->session_id}'";
mysql_query($sql);
$this->expire();
}
}
/*
TABLE STRUCTURE
CREATE TABLE IF NOT EXISTS tb_session_db (
session_id varchar(32) NOT NULL,
value blob,
updated_on datetime DEFAULT NULL,
PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
*/
?>
With this file included, connected to MySQL database who has the table tb_session_db I
can easy do that into my config file:
<?
require_once('session_db.php');
define('MYSQL_HOST','mysql.tricko.com.br');
define('MYSQL_USER','.......');
define('MYSQL_PASS','.......');
define('MYSQL_BASE','.......');
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Cannot connect');
mysql_select_db(MYSQL_BASE) or die('Cannot select DB');
mysql_set_charset('utf8_general_ci');
$SESSION = null;
global $SESSION;
$session_db = new SessionDB();
?>
To use $SESSION on a function/class method, you must call:
global $SESSION
For who will not work with this over classes or functions, it is a very simple/fast way to work!
http://php.net/manual/en/function.session-set-save-handler.php