PHP - saving a session in a database

Hi guys, I am setting up an EC2 cluster with a load balancer. I have a separate database server running mysql. I have 3 web servers working, mainly for high availability, and of course, its load balancing seems to be why every page that you access gets a different server that is losing the session.

I am trying to configure PHP to store it in a database. I installed the table and installed all the functions (open, close, etc.). and I installed:

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

But when I log in to the system or something on the site, I check the table and nothing was written. I am not sure if I need to change something in the php.ini file. If so, what is the significance of the change?

Thank!!

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');
+5
4

, , , .

, (, "_open" "_close" ), -, , .

:

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
}
+1

, , , mysql .

: memcache. Memcache . Memcache , php memcache !!! Memcache , , .

: http://kevin.vanzonneveld.net/techblog/article/enhance_php_session_management/

!!

0

cookie? script , cookie, . .. cookie . , , .

0

- Redis MonogDB, MySQL, memcached. memcached , , Redis MongoDB - . , EC2, Amazon SimpleDB. /, .

MongoDB, , , , MySQL , Mongo... , , , MongoDB =)

HAProxy " ". .

0
source

All Articles