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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
<?php
/* Create new object of class */
$ses_class = new session();
/* Change the save_handler to use the class functions */
session_set_save_handler (array(&$ses_class, '_open'),
array(&$ses_class, '_close'),
array(&$ses_class, '_read'),
array(&$ses_class, '_write'),
array(&$ses_class, '_destroy'),
array(&$ses_class, '_gc'));
/* Start the session */
session_start();
class session
{
/* Define the mysql table you wish to use with
this class, this table MUST exist. */
var $ses_table = "sessions";
/* Change to 'Y' if you want to connect to a db in
the _open function */
var $db_con = "Y";
/* Configure the info to connect to MySQL, only required
if $db_con is set to 'Y' */
var $db_host = "mysql5-3";
var $db_user = "chaletde001";
var $db_pass = "FGpBz2lx";
var $db_dbase = "chaletde001";
/* Create a connection to a database */
function db_connect() {
$mysql_connect = @mysql_pconnect ($this->db_host,
$this->db_user,
$this->db_pass);
$mysql_db = @mysql_select_db ($this->db_dbase);
if (!$mysql_connect || !$mysql_db) {
return FALSE;
} else {
return TRUE;
}
}
/* Open session, if you have your own db connection
code, put it in here! */
function _open($path, $name) {
if ($this->db_con == "Y") {
$this->db_connect();
}
return TRUE;
}
/* Close session */
function _close() {
/* This is used for a manual call of the
session gc function */
$this->_gc(0);
return TRUE;
}
/* Read session data from database */
function _read($ses_id) {
$session_sql = "SELECT * FROM " . $this->ses_table
. " WHERE ses_id = '$ses_id'";
$session_res = @mysql_query($session_sql);
if (!$session_res) {
return '';
}
$session_num = @mysql_num_rows ($session_res);
if ($session_num > 0) {
$session_row = mysql_fetch_assoc ($session_res);
$ses_data = $session_row["ses_value"];
return $ses_data;
} else {
return '';
}
}
/* Write new data to database */
function _write($ses_id, $data) {
$session_sql = "UPDATE " . $this->ses_table
. " SET ses_time='" . time()
. "', ses_value='$data' WHERE ses_id='$ses_id'";
$session_res = @mysql_query ($session_sql);
if (!$session_res) {
return FALSE;
}
if (mysql_affected_rows ()) {
return TRUE;
}
$session_sql = "INSERT INTO " . $this->ses_table
. " (ses_id, ses_time, ses_start, ses_value)"
. " VALUES ('$ses_id', '" . time()
. "', '" . time() . "', '$data')";
$session_res = @mysql_query ($session_sql);
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
/* Destroy session record in database */
function _destroy($ses_id) {
$session_sql = "DELETE FROM " . $this->ses_table
. " WHERE ses_id = '$ses_id'";
$session_res = @mysql_query ($session_sql);
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
/* Garbage collection, deletes old sessions */
function _gc($life) {
$ses_life = strtotime("-5 minutes");
$session_sql = "DELETE FROM " . $this->ses_table
. " WHERE ses_time < $ses_life";
$session_res = @mysql_query ($session_sql);
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
}
?> |
Partager