数据库中的 PHP 会话 [英] PHP sessions in Database

查看:26
本文介绍了数据库中的 PHP 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中存储会话?我是用这段代码做到的:

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?

推荐答案

我很久以前就不得不处理这个问题,有 2 个解决方案,都不是很好,但就这样了.

I had to deal with this along time ago, and there are 2 solutions, neither of which are pretty, but here it goes.

  1. 按照事件的顺序(松散地),您的脚本将检查是否有会话 ID 发送到服务器,然后它将查看该会话是否在您的数据库中,以及该会话是否具有已到期.一旦建立到会话数据库的连接,您就可以考虑对过期日期超过当前时间戳的所有记录运行 DELETE 查询.然后搜索用户刚刚发送的ID.每次人们使用您的网站时都会以这种方式进行清理

  1. 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

下一个方法,你可能会考虑另外做,是有一个 CHRON 作业或其他自动脚本,它每隔一段时间运行一次,以清除会话表中的过期条目.如果您的网站流量不大且不频繁,这是一个很好的方法.

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,但会话已过期,则删除该会话并开始一个新会话.如果您这样做,您就不会遇到太多 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.

确保您的系统是否有某种类型的注销按钮,手动注销的行为将触发用户会话的删除.

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.

这篇关于数据库中的 PHP 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆