Categories

Archives

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

การเก็บ php session ไว้บน mysql

session.inc.php

<?php
function session_open($save_path, $session_name) {
return(true);
}

function session_close() {
return(true);
}

function session_read($id) {
global $sessionlink;
$rs=mysql_query(“SELECT * FROM sessions WHERE id=’$id'”);
if ($rs=mysql_fetch_array($rs)) {
return($rs[‘data’]);
} else {
return(“”);
}
}

function session_write($id, $data) {
global $sessionlink;
$sql=”SELECT id FROM sessions WHERE id=’$id'”;
$rs=mysql_query($sql) or die(“Error: $sql”);
if (mysql_num_rows($rs)>0) {
$sql=”UPDATE sessions SET data='”.addslashes($data).”‘ WHERE id=’$id'”;
} else {
$sql=”INSERT INTO sessions SET id=’$id’,data='”.addslashes($data).”‘”;
}
$writeresult=mysql_query($sql) or die(“Error: $sql”);

if ($writeresult) {
return(true);
} else {
return(false);
}
}

function destroy($id) {
global $sessionlink;
$rs=mysql_query(“DELETE FROM sessions WHERE id=’$id'”);
if ($rs) {
return(true);
} else {
return(false);
}
}

function session_gc($maxlifetime) {
global $sessionlink;
$rs=mysql_query(“DELETE FROM sessions WHERE lasttime<‘”.date(“YmdHis”,time()-$maxlifetime).”‘”);
if ($rs) {
return(true);
} else {
return(false);
}
}

session_set_save_handler(“session_open”,”session_close”,”session_read”,”session_write”,”destroy”,”session_gc”);

?>

 

db_config.inc.php

<?php
mysql_connect(“localhost”,”xxxx”,”yyyy”) or die(“Can’t connect to MYSQL-Server”);
mysql_select_db(“web1001”) or die(“Can’t use database ‘eoffice'”);
mysql_query(“SET NAMES utf8”);
include(“../secure/session.inc.php”);
session_start();

?>

 

สร้าง table session ดังนี้

 create table sessions (id char(32) not null, data text not null, lasttime timestamp, primary key (id));

Comments are closed.