中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

將session保存到數(shù)據(jù)庫的php代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
<?php
//
// 'sessions' table schema
// create table sessions (
//   session_id char(32) not null,
//   session_data text not null,
//   session_expiration int(11) unsigned not null,
//   primary key (session_id));
//

include_once 'DB.php';

// Global Variables
$dbh = NULL;

function on_session_start ($save_path, $session_name) {
    global $dbh;
    $dbh = DB::connect('mysql://user:secret@localhost/SITE_SESSIONS',
                       true);

    if (DB::isError($dbh)) {
        die(sprintf('Error [%d]: %s',
                    $dbh->getCode(), $dbh->getMessage()));
    }
}

function on_session_end ()
{
   // Nothing needs to be done in this function
   // since we used persistent connection.
}

function on_session_read ($key)
{
    global $dbh;

    $stmt = "select session_data from sessions";
    $stmt .= " where session_id = '$key'";
    $stmt .= " and session_expiration > now()";

    $sth = $dbh->query($sth);
    $row = $sth->fetchRow(DB_FETCHMODE_ASSOC);
    return $row['session_data'];
}

function on_session_write ($key, $val)
{
    global $dbh;

    $val = addslashes($val);

    $insert_stmt = "insert into sessions values('$key', '$val', now() + 3600)";
    $update_stmt = "update sessions set session_data = '$val', ";
    $update_stmt .= "session_expiration = now() + 3600 ";
    $update_stmt .= "where session_id = '$key'";

    // First we try to insert, if that doesn't succeed, it means
    // session is already in the table and we try to update
    if (DB::isError($dbh->query($insert_stmt)))
        $dbh->query($update_stmt);
}

function on_session_destroy ($key)
{
    global $dbh;

   $stmt = "delete from sessions where session_id = '$key'";
   $dbh->query($stmt);
}

function on_session_gc ($max_lifetime)
{
    global $dbh;

    // In this example, we don't use $max_lifetime parameter
    // We simply delete all sessions that have expired
    $stmt = "delete from sessions where session_expiration < now()";
    $dbh->query($stmt);
}

session_start ();

// Register the $counter variable as part
// of the session
session_register ("counter");

// Set the save handlers
session_set_save_handler ("on_session_start",   "on_session_end",
                          "on_session_read",    "on_session_write",
                          "on_session_destroy", "on_session_gc");

// Let's see what it does
$counter++;
print $counter;
session_destroy();
?>

標(biāo)簽: Mysql

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:php可逆的加密類

下一篇:python獲取文件夾大小的代碼