- 相關(guān)推薦
php使用MySQL保存session會(huì)話
文章主要介紹了php使用MySQL保存session會(huì)話的方法,涉及php操作session及數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下。
本文實(shí)例講述了php使用MySQL保存session會(huì)話的方法。分享給大家供大家參考。具體分析如下:
在很多大的系統(tǒng)中一般都有這個(gè)功能,但是要分離出來(lái)分析,網(wǎng)上的資料也不太多 這里我整理了一篇發(fā)出來(lái)與大家分享
使用MySQL保存session會(huì)話較files有很多優(yōu)點(diǎn):
1) 有利于分布式系統(tǒng),files只能保存在一臺(tái)機(jī)器上
2) 有利于大訪問(wèn)量的系統(tǒng),使用files時(shí)每個(gè)session保存在一個(gè)文件中,目錄會(huì)超級(jí)大,查找session文件會(huì)比較困難。
使用MySQL保存會(huì)話首先要?jiǎng)?chuàng)建session表:
?
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
<?php
$hostname_login = "localhost"; // Server address
$username_login = "root"; // User name
$password_login = ""; // Password
//
$data_name = "session"; // Database name
$login = mysql_pconnect($hostname_login, $username_login, $password_login) or trigger_error(mysql_error(),E_USER_ERROR);
$sql="SHOW DATABASES LIKE '".$data_name."'"; // If it is exist
if($rs_table=mysql_query($sql,$login)) {
if($rs_value=mysql_fetch_array($rs_table)) {
echo "數(shù)據(jù)庫(kù)已經(jīng)存在!\n!";
exit();
}
}
$sql="CREATE DATABASE $data_name";
mysql_query($sql); // Crate database
echo "數(shù)據(jù)庫(kù)創(chuàng)建成功!\n";
mysql_select_db($data_name, $login);
$sql="CREATE TABLE `sessions` (
`SessionKey` varchar(32) NOT NULL default '',
`SessionArray` blob NOT NULL,
`SessionExpTime` int(20) unsigned NOT NULL default '0',
PRIMARY KEY (`SessionKey`),
KEY `SessionKey` (`SessionKey`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8"; //新建數(shù)據(jù)庫(kù) sql語(yǔ)句
mysql_query($sql);
echo "成功新建數(shù)據(jù)庫(kù)表!";
?>
MysqlSession 類如下:
?
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
<?php
class MysqlSession {
// 注意在有使用Session的頁(yè)面。頁(yè)面一定要頂格,頁(yè)面開(kāi)始處不能留空。
private $DB_SERVER = "localhost"; // 數(shù)據(jù)庫(kù)服務(wù)器主機(jī)名
private $DB_NAME = ""; // 數(shù)據(jù)庫(kù)名字
private $DB_USER = "root"; // MYSQL 數(shù)據(jù)庫(kù)訪問(wèn)用戶名
private $DB_PASS = ""; // MYSQL 數(shù)據(jù)庫(kù)訪問(wèn)密碼
private $DB_SELECT_DB = "";
//private $SESS_LIFE = 1440; // Session的最大使用時(shí)長(zhǎng),單位秒。
private $SESS_LIFE = 0;
function MysqlSession (&$sessionDB) {
//session_write_close();
$this->DB_NAME = &$sessionDB;
$this->SESS_LIFE = get_cfg_var("session.gc_maxlifetime");
session_module_name('user');
session_set_save_handler(
array(&$this, 'sess_open'),
array(&$this, 'sess_close'),
array(&$this, 'sess_read'),
array(&$this, 'sess_write'),
array(&$this, 'sess_destroy'),
array(&$this, 'sess_gc')
);
session_start();
}
function sess_open($save_path, $session_name){ // 打開(kāi)數(shù)據(jù)庫(kù)連接
if (! $this->DB_SELECT_DB = mysql_pconnect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS)) {
echo "SORRY! MYSQL ERROR : Can't connect to $this->DB_SERVER as $DB_USER";
echo "MySQL Error: ", mysql_error();
die;
}
if (! mysql_select_db($this->DB_NAME, $this->DB_SELECT_DB)) {
echo "SORRY! MYSQL ERROR : Unable to select database $this->DB_NAME";
die;
}
return true;
}
function sess_close() {
return true;
}
function sess_read($SessionKey){
$Query = "SELECT SessionArray FROM sessions WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time();
// 過(guò)期不讀取。
$Result = mysql_query($Query, $this->DB_SELECT_DB);
if (list($SessionArray) = mysql_fetch_row($Result)) {
return $SessionArray;
}
return false;
}
function sess_write($SessionKey, $VArray) {
$SessionExpTime = time() + $this->SESS_LIFE;
// 更新Session過(guò)期時(shí)間,Session過(guò)期時(shí)間 = 最后一次更新時(shí)間 + Session的最大使用時(shí)長(zhǎng)
$SessionArray = addslashes($VArray);
$Query = "INSERT INTO sessions (SessionKey,SessionExpTime,SessionArray) VALUES ('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')";
$Result = mysql_query($Query, $this->DB_SELECT_DB);
if (!$Result){
$Query = "UPDATE sessions SET SessionExpTime = '".$SessionExpTime."', SessionArray = '".$SessionArray."' WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time();
$Result = mysql_query($Query, $this->DB_SELECT_DB);
}
return $Result;
}
function sess_destroy($SessionKey) {
$Query = "DELETE FROM sessions WHERE SessionKey = '".$SessionKey."'";
$Result = mysql_query($Query, $this->DB_SELECT_DB);
return $Result;
}
function sess_gc($maxlifetime) {
// 這個(gè)垃圾清除器系統(tǒng)調(diào)用。默認(rèn)是1440秒清除一次。
//參數(shù)可以在PHP.ini里面設(shè)置。
$Query = "DELETE FROM sessions WHERE SessionExpTime < " . time();
$Result = mysql_query($Query, $this->DB_SELECT_DB);
return mysql_affected_rows($this->DB_SELECT_DB);
}
}
?>
用法:在原來(lái)使用 session_start 的地方,替換成 $session = new MysqlSession ()
注意:包含此程序前要打開(kāi)數(shù)據(jù)庫(kù),主程序退出前不能關(guān)閉數(shù)據(jù)庫(kù),否則會(huì)出錯(cuò)。
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
【php使用MySQL保存session會(huì)話】相關(guān)文章:
PHP會(huì)話session 時(shí)間設(shè)定使用入門10-04
PHP中使用session實(shí)現(xiàn)保存用戶登錄信息09-07
Session在PHP中的使用07-24
如何使用php中session08-27
教你如何使用php的session07-13
PHP中使用會(huì)話控制11-21
PHP中session使用方法詳解08-29
PHP創(chuàng)建和使用session cookie變量05-16
WEB中使用PHP連接MySQL的方法09-27