php中的实现session监听

session_set_save_handler

$SESSION_LIFE = get_cfg_var("session.gc_maxlifetime");
//$SESSION_LIFE = 1440;

function sess_open ($save_path, $session_name)
{
    global $sess_save_path, $sess_session_name;

    $sess_save_path = $save_path;
    $sess_session_name = $session_name;
    return(true);
}

function sess_close()
{
    return(true);
}

function sess_read ($key)
{
    global $sess_save_path, $sess_session_name, $SESSION_LIFE, $sqlca;

    $query = "SELECT session_value FROM ts_sessions WHERE session_key = '$key' AND session_expiry > " . time();
    //echo "query='$query'<br>";
    $sqlca->query($query);

    if ($sqlca->next_record())
    {
        return $sqlca->f(0);
    }
    else
    {
        return("");
    }
}

function sess_write ($key, $val)
{
    global $sess_save_path, $sess_session_name, $SESSION_LIFE, $sqlca;


    $expiry = time() + $SESSION_LIFE;
    //$value = addslashes($val);
    $value = $val;

    $sqlca->query("SELECT session_key,session_expiry FROM ts_sessions WHERE session_key='$key'");
    if($sqlca->next_record())
    {
        $query = "UPDATE ts_sessions SET session_value = '$value', session_expiry = $expiry WHERE session_key = '$key' AND session_expiry > " . time();
    }
    else
    {
        $query = "INSERT ts_sessions VALUES('$key', '$value', $expiry)";
    }

    //echo "query='$query'<br>";
    $sqlca->query($query);

    return $sqlca->Query_ID;
}

function sess_update_time ($key)
{
    global $SESSION_LIFE, $sqlca;

    $expiry = time() + $SESSION_LIFE;

    $query = "UPDATE ts_sessions SET session_expiry = $expiry WHERE session_key = '$key'";
    return $sqlca->query($query);
}

function sess_destroy ($key)
{
    global $sqlca;

    $query = "DELETE FROM ts_sessions WHERE session_key = '$key'";
    $sqlca->query($query);

    return $sqlca->Query_ID;
}

function sess_gc ($maxlifetime)
{
    global $sqlca;

    $query = "DELETE FROM ts_sessions WHERE session_expiry < " . time();
    $sqlca->query($query);

    return $sqlca->Query_ID;
}

session_set_save_handler ("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();

 

from http://www.qiuhao.com/PHP10000%5C10276.htm

现在记下,以备日后用到

你可能感兴趣的:(PHP,F#)