0

A company that I'm working now is going to use DB sessions to organize their servers. I have no choice to refuse it :(

The sample php code they were gonna use was

sessioninit.php

<?
/* ------------------------------------------------------------------------
* Create a new database in MySQL called "sessions" like so:
*
* CREATE TABLE ucnovel_session (
*      sesskey char(32) not null,
*      expiry int(11) unsigned not null,
*      value text not null,
*      PRIMARY KEY (sesskey)
* );
*/

$SESS_DBH = "";
$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");


function sess_open($save_path, $session_name)
{
    global $SESS_DBH;

    $sv = "192.168.0.3";    
    $SESS_DBH = mysql_pconnect($sv, "ucnovel", "비번") or die("Can't connect to SQL Server");
    mysql_select_db("hotdog", $SESS_DBH) or die("Can't connect to SQL Server");

    return true;
}

function sess_close()
{
    return true;
}

function sess_read($key)
{
    global $SESS_DBH, $SESS_LIFE;

    //echo $key;

    $qry = "SELECT value FROM ucnovel_session WHERE sesskey = '$key' AND expiry > " . time();
    $qid = mysql_query($qry, $SESS_DBH);

    if (list($value) = mysql_fetch_row($qid)) {
        return $value;
    }

    return false;
}

function sess_write($key, $val)
{
    global $SESS_DBH, $SESS_LIFE;

    $expiry = time() + $SESS_LIFE;
    $value = addslashes($val);

    $qry = "SELECT COUNT(*) FROM ucnovel_session WHERE sesskey='$key'";
    $qid = mysql_fetch_array(mysql_query($qry, $SESS_DBH));
    if($qid[0]>0) {
        $qry = "UPDATE ucnovel_session SET expiry = $expiry, value = '$value' WHERE sesskey = '$key'"; // AND expiry > " . time();
        $qid = mysql_query($qry, $SESS_DBH);
    } else {
        $qry = "INSERT INTO ucnovel_session SET expiry = $expiry, value = '$value', sesskey = '$key'";
        $qid = mysql_query($qry, $SESS_DBH);
    }

    return $qid;
}

function sess_destroy($key)
{
    global $SESS_DBH;

    $qry = "DELETE FROM ucnovel_session WHERE sesskey = '$key'";
    $qid = mysql_query($qry, $SESS_DBH);

    return $qid;
}

function sess_gc($maxlifetime)
{
    global $SESS_DBH;

    $qry = "DELETE FROM ucnovel_session WHERE expiry < " . time();
    $qid = mysql_query($qry, $SESS_DBH);

    return mysql_affected_rows($SESS_DBH);
}

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_set_cookie_params(0, "/", "ucnovel.com", false, false);

session_start();


?>

sessiontest.php

<?
include "sessioninit.php";

$old = $_COOKIE[test_sess2];
$new = rand()%10000;
$_COOKIE[test_sess2] = $new;
setcookie("test_sess2", $new);

echo "OLD ".$old."<br>"."NEW ".$new;

$old = $_SESSION[test_sess];
$new = rand()%10000;
$_SESSION[test_sess] = $new;
$_SESSION[test_ip] = $REMOTE_ADDR;

echo "<br><br>OLD ".$old."<br>"."NEW ".$new;

?>

I understand that they override the session handler and customized it. But I can't find any references on how to do it in Java.

Some people says that overriding HttpSession is a way. But I still can't understand how to do this. Also, I don't know if DTOs will save in the DB :S

I'm using Spring 3.1 right now, and the server is MySQL 5.5

Is there any samples that I can understand how to do it? or maybe a plugin that helps it?

Passerby
  • 9,715
  • 2
  • 33
  • 50
Steve Gom
  • 75
  • 3
  • 9

0 Answers0