本文实例讲述了php使用MySQL保存session会话的方法。分享给大家供大家参考。具体分析如下:
在很多大的系统中一般都有这个功能,但是要分离出来分析,网上的资料也不太多 这里我整理了一篇发出来与大家分享
使用MySQL保存session会话较files有很多优点:
1) 有利于分布式系统,files只能保存在一台机器上
2) 有利于大访问量的系统,使用files时每个session保存在一个文件中,目录会超级大,查找session文件会比较困难。
使用MySQL保存会话首先要创建session表
$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 "数据库已经存在!\n!";
exit();
}
}
$sql="CREATE DATABASE $data_name";
mysql_query($sql); // Crate database
echo "数据库创建成功!\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"; //新建数据库 sql语句
mysql_query($sql);
echo "成功新建数据库表!";
?>
$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 "数据库已经存在!\n!";
exit();
}
}
$sql="CREATE DATABASE $data_name";
mysql_query($sql); // Crate database
echo "数据库创建成功!\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"; //新建数据库 sql语句
mysql_query($sql);
echo "成功新建数据库表!";
?>
session_module_name
(PHP 4, PHP 5)
session_module_name — 获取/设置会话模块名称
说明¶
string session_module_name ([ string $module ] )
session_module_name() 获取或设置会话模块名称。
参数¶
module
如果指定 module 参数,则使用 指定值作为会话模块。
返回值¶
返回当前所用的会话模块名称。
User Contributed Notes 3 notes
0
2 months ago
0
7 months ago
This function is used to set the Session Module at site or script level.
The global configuration can be done in php.ini under the [Session] section and with the name of "session.save_handler". The sessions are saved in files by default, like so:
session.save_handler = files
But with this configuration you set one of your websites to use some other session module (if you have them installed and extension loaded with PHP), like so:
// You'll need to define a save path also, if the module is other than files, like so:session_save_path('localhost:11211');// memcache uses port 11211
// or you can use multiple for load balancing:session_save_path('localhost:11211:41,otherhost:11211:60')// First part is hostname or path to socket, next is port and the last is the weight for that server
//The function also returns the value of the current session module.echosession_module_name();// will print memcache in our case
// or maybe a checkif(session_module_name() !='memcache'){// Do something, throw an exception maybe}
-3
1 year ago
session_start();$_SESSION['name']="Tushar";$n=$_SESSION['name'];$_SESSION['Age']="23";$_SESSION['city']="Tarapur";//echo session_encode()."
";//Prints all Session Data
//echo session_is_registered($n);echosession_module_name();//it prints "files"?>
output:
files