gpt4 book ai didi

php - 在sql中存储session (session_set_save_handler())

转载 作者:行者123 更新时间:2023-11-30 23:36:03 24 4
gpt4 key购买 nike

所以,我正在制作一个登录系统,用户应该登录超过 24 分钟。这就是我决定将 session 存储在我的数据库中的原因。 我使用了这个函数:(我建立了自己的sql连接-> sqlConnectionConfig.php)
sessions.php 文件(我在 php.net 上找到了这段代码。):

class session {
// session-lifetime
var $lifeTime;
// mysql-handle
var $dbHandle;
function open($savePath, $sessName) {
// get session-lifetime
$this->lifeTime = get_cfg_var("session.gc_maxlifetime");
// open database-connection
require_once '../sqlConnectionConfig.php';
$dbHandle = @mysql_connect($sqlHost,$sqlUser,$sqlPass);
$dbSel = @mysql_select_db($sqlDb,$dbHandle);
//return success
if(!$dbHandle || !$dbSel)
return false;
$this->dbHandle = $dbHandle;
return true;
}
function close() {
$this->gc(ini_get('session.gc_maxlifetime'));
// close database-connection
return @mysql_close($this->dbHandle);
}
function read($sessID) {
// fetch session-data
$res = mysql_query("SELECT session_data AS d FROM ws_sessions
WHERE session_id = '$sessID'
AND session_expires > ".time(),$this->dbHandle);
// return data or an empty string at failure
if($row = mysql_fetch_assoc($res))
return $row['d'];
return "";
}
function write($sessID,$sessData) {
// new session-expire-time
$newExp = time() + $this->lifeTime;
// is a session with this id in the database?
$res = mysql_query("SELECT * FROM ws_sessions
WHERE session_id = '$sessID'",$this->dbHandle);
// if yes,
if(mysql_num_rows($res)) {
// ...update session-data
mysql_query("UPDATE ws_sessions
SET session_expires = '$newExp',
session_data = '$sessData'
WHERE session_id = '$sessID'",$this->dbHandle);
// if something happened, return true
if(mysql_affected_rows($this->dbHandle))
return true;
}
// if no session-data was found,
else {
// create a new row
mysql_query("INSERT INTO ws_sessions (
session_id,
session_expires,
session_data)
VALUES(
'$sessID',
'$newExp',
'$sessData')",$this->dbHandle);
// if row was created, return true
if(mysql_affected_rows($this->dbHandle))
return true;
}
// an unknown error occured
return false;
}
function destroy($sessID) {
// delete session-data
mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle);
// if session was deleted, return true,
if(mysql_affected_rows($this->dbHandle))
return true;
// ...else return false
return false;
}
function gc($sessMaxLifeTime) {
// delete old sessions
mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle);
// return affected rows
return mysql_affected_rows($this->dbHandle);
}
}



我的 admin/index.php 和 admin/admin.php 文件都有相同的 session_start():

   ini_set("session.gc_maxlifetime", "18000");
require_once 'sessions.php';
$session = new session();
session_set_save_handler(array(&$session,"open"),
array(&$session,"close"),
array(&$session,"read"),
array(&$session,"write"),
array(&$session,"destroy"),
array(&$session,"gc"));
session_start();

index.php 包含表单并通过 post 将登录数据发送到 admin.php。
问题是:它会在 24 分钟后注销。但我不知道为什么,我的数据库成功存储了 session 。

最佳答案

我发现,这一行导致了问题:

 $this->lifeTime = get_cfg_var("session.gc_maxlifetime");

所以,我手动设置了它。 ($this->lifeTime = 18000;)

关于php - 在sql中存储session (session_set_save_handler()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7269624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com