简体   繁体   English

PHP - 在数据库中存储会话

[英]PHP - Storing Session in a Database

I am setting up a EC2 cluster with a load balancer.我正在设置一个带有负载均衡器的 EC2 集群。 I have seperate DB server which has mysql running on it.我有单独的数据库服务器,上面运行着 mysql。 I have 3 webservers running, mostly for high availability, and of course its round-robin load balancing it seems, so every page you go to you get a different server which looses your session.我有 3 个 web 服务器在运行,主要是为了高可用性,当然它似乎是循环负载平衡,所以你访问的每个页面都会得到一个不同的服务器,这会丢失你的会话。

I am trying to setup PHP to store it in the DB.我正在尝试设置 PHP 以将其存储在数据库中。 I have setup a table, and setup all the functions (open,close,etc).我已经设置了一个表格,并设置了所有功能(打开、关闭等)。 and I have set:我已经设置:

session_set_save_handler('_open',
                         '_close',
                         '_read',
                         '_write',
                         '_destroy',
                         '_clean');

But when I login or anything on the site I check the table and nothing has been written.但是当我登录或网站上的任何内容时,我检查了表格,但没有写入任何内容。 I am not sure If i need to change something in the php.ini file.我不确定是否需要更改 php.ini 文件中的某些内容。 If so what is the value to change?如果是这样,要改变的价值是什么?

Thanks!!谢谢!!

EDIT: functions:编辑:功能:

function _open(){    
    global $con;
    connect();
} 
    
function _close(){  
    global $con;  
    //mysql_close();
}

function _read($id){    
    global $con;    
    $id = mysql_real_escape_string($id);     
    $sql = "SELECT data FROM sessions WHERE id = '$id'";     
    if ($result = mysql_query($sql, $con)) {        
        if (mysql_num_rows($result)) {            
            $record = mysql_fetch_assoc($result);             
            return $record['data'];        
        }    
    }     
    return '';
}

function _write($id, $data)
{
    global $con;
 
    $access = time();
 
    $id = mysql_real_escape_string($id);
    $access = mysql_real_escape_string($access);
    $data = mysql_real_escape_string($data);
 
    $sql = "REPLACE
            INTO    sessions
            VALUES  ('$id', '$access', '$data')";
 
    return mysql_query($sql, $con);
}

function _destroy($id)
{
   global $con;
    $id = mysql_real_escape_string($id);
    $sql = "DELETE
            FROM   sessions
            WHERE  id = '$id'";
    return mysql_query($sql, $con);
}

function _clean($max)
{
    global $con;
 
    $old = time() - $max;
    $old = mysql_real_escape_string($old);
 
    $sql = "DELETE
            FROM   sessions
            WHERE  access < '$old'";
 
    return mysql_query($sql, $con);
}

session_set_save_handler('_open',
                         '_close',
                         '_read',
                         '_write',
                         '_destroy',
                         '_clean');

The function you're using will interrupt the normal session function and insert your code in to act, then carry on with other internal functions.您正在使用的函数将中断正常的会话函数并插入您的代码来执行,然后继续执行其他内部函数。

In the functions where you aren't really doing anything session-wise (things like '_open' and '_close'), you need return something to carry on the command or it will just die right there.在你没有真正做任何会话的函数中(比如“_open”和“_close”),你需要返回一些东西来执行命令,否则它就会死在那里。

Example:例子:

function _open($save_path, $session_name){    
    global $con;
    connect();

    return true; //Do nothing but carry on
} 

function _close(){  
    global $con;  
    //mysql_close();

    return true; //Do nothing but carry on
}

Really this is not the answer to your question, but if you need really high availability I can suggest that storing sessions in mysql is not a good method.这确实不是您问题的答案,但是如果您需要真正的高可用性,我可以建议将会话存储在 mysql 中不是一个好方法。

A better practice: Put your sessions in memcache.更好的做法:将会话放在内存缓存中。 Memcache server it's easy to install. Memcache 服务器很容易安装。 Memcache client is easy to install and php support storing session in memcache without programming!!! Memcache 客户端安装简单,php 支持在 memcache 中存储 session,无需编程!!! Memcache store data in memory, not in disk and it's faster. Memcache 将数据存储在内存中,而不是磁盘中,而且速度更快。

Here you can view a good post about this: http://kevin.vanzonneveld.net/techblog/article/enhance_php_session_management/在这里您可以查看关于此的好帖子: http : //kevin.vanzonneveld.net/techblog/article/enhance_php_session_management/

Luck!!运气!!

How come you can't use cookies?为什么你不能使用cookies? and just have the script check if the cookies is present then read the value's from there.只需让脚本检查 cookie 是否存在,然后从那里读取值。 Unless security is an issue .. I suggest cookies over sessions for cheap scalability ..Writing session's in the database won't be good and will probably result in expensive queries being ran for no reason.除非安全是一个问题.. 我建议在会话上使用 cookie 以获得廉价的可扩展性..在数据库中写入会话不会很好,并且可能会导致无缘无故地运行昂贵的查询。

The best database for sessions is going to be something like Redis or MonogDB and not MySQL and not memcached.会话的最佳数据库将是 Redis 或 MonogDB 之类的数据库,而不是 MySQL 和 memcached。 You could use memcached depending on how much traffic you get, but Redis or MongoDB is a much more scalable database.您可以根据获得的流量使用 memcached,但 Redis 或 MongoDB 是更具可扩展性的数据库。 Also, since you're on EC2, you may want to look at Amazon SimpleDB.此外,由于您使用的是 EC2,您可能需要查看 Amazon SimpleDB。 It's a key/value store so it should lend itself well to sessions.它是一个键/值存储,因此它应该非常适合会话。

I'd personally run with MongoDB because not only can you store your sessions there, but you can take what you do have from MySQL and cache it there too as well as store other data in Mongo...Or just think about converting your site's database altogether because you'll probably fall in love with MongoDB =)我个人会使用 MongoDB 运行,因为您不仅可以将会话存储在那里,而且您可以从 MySQL 中获取您拥有的内容并将其缓存在那里,以及将其他数据存储在 Mongo 中......或者只是考虑转换您网站的数据库,因为你可能会爱上 MongoDB =)

You can also look into the settings of HAProxy under Application Cookies.您还可以在 Application Cookies 下查看 HAProxy 的设置。 That may help you too.这也可能对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM