我在这里搜索了这个问题..但我无法得到正确答案......所以请仔细阅读我的问题&请回答我...谢谢是提前..
-------------- Session.php ------------------------
<?php
class WSession
{
function __construct() {
}
function sess_open($sess_path, $sess_name) {
return true;
}
function sess_close() {
return true;
}
function sess_read($sess_id) {
mysql_connect("test", "test", "test");
mysql_select_db("test");
$result = mysql_query("SELECT Data FROM sessions WHERE SessionID = '$sess_id';");
if (!mysql_num_rows($result)) {
//Session found, pull out data field value
$sess_Data = $results->data;
$CurrentTime = time();
mysql_query("INSERT INTO sessions (SessionID, DateTouched) VALUES ('$sess_id', $CurrentTime);");
return $sess_Data;
} else {
$CurrentTime = time();
extract(mysql_fetch_array($result), EXTR_PREFIX_ALL, 'sess');
mysql_query("UPDATE sessions SET DateTouched = $CurrentTime WHERE SessionID = '$sess_id';");
return "";
}
}
function sess_write($sess_id, $data) {
mysql_connect("test", "test", "test");
mysql_select_db("test");
$CurrentTime = time();
mysql_query("UPDATE sessions SET Data = '$data', DateTouched = $CurrentTime WHERE SessionID = '$sess_id';");
return true;
}
function sess_destroy($sess_id) {
mysql_connect("test", "test", "test");
mysql_select_db("test");
mysql_query("DELETE FROM sessions WHERE SessionID = '$sess_id';");
return true;
}
function sess_gc($sess_maxlifetime) {
mysql_connect("test", "test", "test");
mysql_select_db("test");
$CurrentTime = time();
mysql_query("DELETE FROM sessions WHERE DateTouched + $sess_maxlifetime < $CurrentTime;");
return true;
}
}
?>
------------------- Index.php ---------(MAIN FILE)----------------
require_once(session.php);
$handler = new WSession();
session_set_save_handler(array($handler, "sess_open"),
array($handler, "sess_close"),
array($handler, "sess_read"),
array($handler, "sess_write"),
array($handler, "sess_destroy"),
array($handler, "sess_gc"));
register_shutdown_function('session_write_close');
session_start();
$_SESSION['test'] = "testado";
然后是这个,我们正在调用登录控制器(例如:login.php)。
---------------------- login.php ----------------------
重定向后,我们在这里尝试使用save handler保存会话。
$_SESSION['username'] = "XXXXXX";
$_SESSION['testing'] = "XXXXXX";
但它没有将上述值写入DB。
它只存储主文件中的第一个数据
test|s:9:"testado";
您能否指导保存其他值。
答案 0 :(得分:0)
我正在努力实现类似的目标。但是我正在使用PDO,我在类Session中的构造函数中调用了session_set_save_handler。
看看这个简单的教程http://www.devshed.com/c/a/PHP/Storing-PHP-Sessions-in-a-Database/
在将内容设置为$ _SESSION之前,请确保您的会话在login.php上运行。