当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

将session存储到数据库中

发表于: 2015-07-14   作者:dcj3sjt126com   来源:转载   浏览:
摘要: CREATE TABLE sessions ( id CHAR(32) NOT NULL, data TEXT, last_accessed TIMESTAMP NOT NULL, PRIMARY KEY (id) );   <?php /** * Created by PhpStorm. * User: michaeldu * Date
CREATE TABLE sessions (
  id CHAR(32) NOT NULL,
  data TEXT,
  last_accessed TIMESTAMP NOT NULL,
  PRIMARY KEY (id)
);

 

<?php
/**
 * Created by PhpStorm.
 * User: michaeldu
 * Date: 15/7/14
 * Time: 下午2:57
 */
$sdbc = NULL;

function open_session() {
    global $sdbc;
    $sdbc = mysqli_connect('192.168.31.172', 'root', 'root', 'phpadvanced');
    return true;
}

function close_session() {
    global $sdbc;
    return mysqli_close($sdbc);
}

function read_session($sid) {
    global $sdbc;
    $q = sprintf('SELECT data FROM sessions WHERE  id="%s"', mysqli_real_escape_string($sdbc, $sid));
    $r = mysqli_query($sdbc, $q);

    if (mysqli_num_rows($r) == 1) {
        list($data) = mysqli_fetch_array($r, MYSQLI_NUM);
        return $data;
    } else {
        return '';
    }
}

function write_session($sid, $data) {
    global $sdbc;

    $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")', mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data));
    $r = mysqli_query($sdbc, $q);
    return true;
}

function destroy_session($sid) {
    global $sdbc;
    $q = sprintf('DELETE FROM session WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid));
    $r = mysqli_query($sdbc, $q);

    $_SESSION = array();

    return true;
}

function clean_session($expire) {
    global $sdbc;

    $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()', (int)$expire);
    $r = mysqli_query($sdbc, $q);

    return true;
}

session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');

session_start();

 

<?php
/**
 * Created by PhpStorm.
 * User: michaeldu
 * Date: 15/7/14
 * Time: 下午3:14
 */
require('db_session.inc.php');
?>
<!DOCTYPE HTML>
<html>

<head>
    <title>colour_blue</title>
    <meta name="description" content="website description" />
    <meta name="keywords" content="website keywords, website keywords" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="/phpadvanced/02developwebapplication/style/style.css" title="style" />
</head>

<body>
<?php
if(empty($_SESSION)) {
    $_SESSION['blah'] = 'umlaut';
    $_SESSION['this'] = 3615684.45;
    $_SESSION['that'] = 'blue';
    echo '<p>Session已存储</p>';
} else {
    echo '<p>SESSION已存在, <pre>'.print_r($_SESSION, 1).'</pre></p>';
}

if(isset($_GET['logout'])) {
    session_destroy();
    echo '<p>会话结束</p>';
} else {
    echo '<a href="sessions.php?logout=true">登出</a>';
}

echo '<p>会话数据: <pre>'.print_r($_SESSION, 1).'</pre></p>';
?>
</body>
</html>
<?php
    session_write_close();
?>

 

将session存储到数据库中

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号