`
jacky2007
  • 浏览: 169492 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

php中的实现session监听

    博客分类:
  • PHP
阅读更多

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

现在记下,以备日后用到

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics