数据库中的 PHP 会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11272405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP sessions in Database
提问by Abadis
How can i store sessions in database ? I did it using this code :
如何在数据库中存储会话?我是用这个代码做到的:
function write ($session_id, $session_data)
// write session data to the database.
{
if (!empty($this->fieldarray)) {
if ($this->fieldarray['session_id'] != $session_id) {
// user is starting a new session with previous data
$this->fieldarray = array();
} // if
} // if
if (empty($this->fieldarray)) {
// create new record
$array['session_id'] = $session_id;
$array['date_created'] = getTimeStamp();
$array['last_updated'] = getTimeStamp();
$array['session_data'] = addslashes($session_data);
$this->_dml_insertRecord($array);
} else {
// update existing record
if (isset($_SESSION['logon_user_id'])) {
$array['user_id'] = $_SESSION['logon_user_id'];
} // if
$array['last_updated'] = getTimeStamp();
$array['session_data'] = addslashes($session_data);
$this->_dml_updateRecord($array, $this->fieldarray);
} // if
return TRUE;
} // write
But how should i remove it automaticaly like before. I mean how to delete sessions after time out automatically?
但是我应该如何像以前一样自动删除它。我的意思是如何在超时后自动删除会话?
回答by Mike
I had to deal with this along time ago, and there are 2 solutions, neither of which are pretty, but here it goes.
很久以前我不得不处理这个问题,有两种解决方案,这两种解决方案都不漂亮,但它就在这里。
In the order of events (loosely), your script will check to see if there was a session ID sent to the server, then it will see if that session is in your database, and if the the session has expired. As soon as you've established the connection to your session db, you might consider running a DELETE query on all records with an expiration date past the current time stamp. Then search for the ID that the user just sent. This way clean up is being performed every time people use your site
The next way, which you might consider doing in addition, is to have a CHRON Job or other automated script which runs every so often to clean expired entries from your session table. This is a good method if your site receives light and infrequent traffic.
按照事件的顺序(松散地),您的脚本将检查是否有会话 ID 发送到服务器,然后它将查看该会话是否在您的数据库中,以及该会话是否已过期。一旦建立到会话数据库的连接,您就可以考虑对过期日期超过当前时间戳的所有记录运行 DELETE 查询。然后搜索用户刚刚发送的ID。每次人们使用您的网站时都会以这种方式进行清理
下一个方法,您可能会考虑另外做,是有一个 CHRON 作业或其他自动脚本,它经常运行以清除会话表中的过期条目。如果您的网站流量较少且不频繁,这是一种很好的方法。
A combination of methods is this. With in your script when you are validating your session, check the expire date when the session data is found. if the ID is found, but the session is expired, delete the session and start a new one. If you do it this way, you won't have to run into too many problems of ID collision or lots of queries slowing down your server. You can still run your chron job at the end each day to run a complete cleanup.
方法的组合是这样的。在您验证会话时的脚本中,检查找到会话数据时的到期日期。如果找到 ID,但会话已过期,则删除该会话并开始一个新会话。如果您这样做,您将不必遇到太多 ID 冲突或大量查询减慢服务器速度的问题。您仍然可以在每天结束时运行您的 chron 作业以运行完整的清理。
Make sure if your system has a logout button of some type that the act of manually logging out will trigger the deletion of the user session.
确保您的系统是否有某种类型的注销按钮,手动注销的行为将触发用户会话的删除。
回答by Andres
Take a look at HTTP_Session2. From PEAR docs:
看看HTTP_Session2。从梨文档:
HTTP_Session2provides extra features such as database storage for session datausing the DB and MDB2 package. It also introduces new methods, such as isNew(), useCookies(), setExpire(), setIdle(), isExpired(), isIdled() and others.
HTTP_Session2提供了额外的功能,例如使用 DB 和 MDB2 包的会话数据的数据库存储。它还引入了新方法,例如 isNew()、useCookies()、setExpire()、setIdle()、isExpired()、isIdled() 等。
If you want to implement yours, look at the code in MDB2.phpinside that package. It contains a garbage collection method (gc), which combined with session_set_save_handlershould do the trick.
如果您想实现您的,请查看MDB2.php该包中的代码。它包含一个垃圾收集方法 (gc),结合session_set_save_handler应该可以解决问题。
回答by Maou
At the end of your code session_destroy();to close your session:
在session_destroy();关闭会话的代码末尾:
<?php
session_destroy();
?>

