下一篇 » « 上一篇

Mysql存取session实例

作者:爱好者    时间:2008-01-22    来源:php之家    点击:1310    本文共1篇文章 字体:[ ]

Mysql存取session实例

files:
common/Common.config.php
include/session.inc.php
session_test.php
get_session_test.php
get_session_test2.php www.444p.com版权所有


Common.config.php
  
<?php
/*
* Common config
* By 恋太后天
*/ php学习之家http://www.444p.com


/*
* Database config
*/
define( "DBTYPE", "mysql" );
$database = array
(
    "mysql" => array
    (
        "default" => array
        (
              "host"     => "localhost",
              "user"     => "root",
              "password" => "",
              "dbname"   => ""
        ),
        "session" => array
        (
              "host"     => "localhost", www.444p.com版权所有
              "user"     => "session",
              "password" => "session",
              "dbname"   => "sessions"
        )
    )
); www.444p.com版权所有

?> php学习之家


session.inc.php

www.444p.com

 
<?php
//使用mysql存放session 函数表
// by 恋太后天 2005-4-28

if (!isset($include_path)) $include_path = '; php学习之家http://www.444p.com

if (!is_array($database))
{
    include ($include_path."common/Common.config.php");
}

php学习之家

$DBsess      = $database[DBTYPE]["session"];
$DBsess_link = mysql_connect($DBsess["host"], $DBsess["user"], $DBsess["password"])
               or die ("Error:<em>Can not connect to Mysql server.</em>");

$SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); 本文来自 www.444p.com

function sess_open($path, $name)
{
    return true;
}

www.444p.com

function sess_close()
{
    return true;
}

www.444p.com php学习之家

function sess_read($id)
{
    global $DBsess , $DBsess_link;
    mysql_select_db($DBsess["dbname"]);
    $now = time();
    $result = mysql_query("SELECT `data` FROM `sessions`
                           WHERE `id`= '$id' AND `expiry_time` > $now", $DBsess_link);   
    if (list($data) = mysql_fetch_row($result))
    {  
       return $data;  
    }  
    return false;
} 本文来自 www.444p.com

function sess_write($id, $data)
{
    global $DBsess , $DBsess_link, $SESS_LIFE;
    mysql_select_db($DBsess["dbname"]);

www.444p.com版权所有

    $expiry_time = time() + $SESS_LIFE;

php学习之家

    if ( !get_magic_quotes_gpc() )
    {
        $data = addslashes($data);
    } www.444p.com php学习之家

    $now = time();

www.444p.com

    $result = mysql_query("INSERT into `sessions` (`id`, `expiry_time`,  `data`)", $DBsess_link); php学习之家

    if ( !$result )
    {
        $result = mysql_query("UPDATE `sessions` SET `data`='$data', `expiry_time`=$expiry_time
                               WHERE `id` = '$id' AND `expiry_time` > $now", $DBsess_link);
    } php学习之家

    return $result;
}

www.444p.com

function sess_destroy($id)
{
    global $DBsess , $DBsess_link;
    mysql_select_db($DBsess["dbname"]);
    $query = mysql_query("DELETE FROM `session` WHERE `id`='$id'");
    return $query;
} www.444p.com

function sess_gc($maxlifetime)
{
    global $DBsess , $DBsess_link; 
    $query = mysql_query("DELETE FROM `sessions` WHERE `expiry_time` < " . time(), $DBsess_link);  
    return mysql_affected_rows($DBsess_link);   www.444p.com

} 本文来自 www.444p.com

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

php学习之家http://www.444p.com

?> www.444p.com版权所有


session_test.php
  
<?php
// test for using session
include ("common/Common.config.php");
include ("include/session.inc.php"); www.444p.com版权所有

session_start(); www.444p.com版权所有

$_SESSION["abc"] = "A: I will be back!";
$_SESSION["meto"] = "B: Me too ";
echo "<a href=\"get_session_test.php\">click me</a>"; www.444p.com

?> php学习之家

get_session_test.php php学习之家http://www.444p.com


  
<?php
// test for using session
include ("common/Common.config.php");
include ("include/session.inc.php");

php学习之家http://www.444p.com

session_start();
/*
* www.knowsky.com
*/
$_SESSION["c"] = "<br>C: I will follow U. ^0^!";
print($_SESSION["abc"]);
print("<br>");
print($_SESSION["meto"]);
echo "<br>".
     "<a href=\"get_session_test2.php\">click again</a>";

?> www.444p.com版权所有


get_session_test2.php php学习之家http://www.444p.com

  
<?php
//get_session_test2.php
// test for using session
include ("common/Common.config.php");
include ("include/session.inc.php");

www.444p.com

session_start();
print($_SESSION["c"]);
?> php学习之家

发表评论
密码: (游客不需要密码)
记住我【Alt+S 或 Ctrl+Enter 快速提交】

搜索工具


《PHP精通》点击排行