发表文章 返回首页
当前位置: bitsCN.com > PHP > PHP技术进阶 >

Mysql存取session实例

时间:2006-02-27 00:00来源:中国网管联盟 作者:bitsCN整理 点击:
files: common/Common.config.php include/session.inc.php session_test.php get_session_test.php get_session_test2.php Common.config.php ?php /* * Common config * By 恋太后天 */ /* * Database config */ define( "DBTYPE", "mysql" ); $database = array ( "
  

files:
common/Common.config.php
include/session.inc.php
session_test.php
get_session_test.php
get_session_test2.php feedom.net


Common.config.php
  
<?php
/*
* Common config
* By 恋太后天
*/

中国网管联盟www.bitscn.com


/*
* Database config
*/
define( "DBTYPE", "mysql" );
$database = array
(
    "mysql" => array
    (
        "default" => array
        (
              "host"     => "localhost",
              "user"     => "root",
              "password" => "",
              "dbname"   => ""
        ),
        "session" => array
        (
              "host"     => "localhost",

54com.cn

              "user"     => "session",
              "password" => "session",
              "dbname"   => "sessions"
        )
    )
); 中国网管论坛bbs.bitsCN.com

?>

中国网管论坛bbs.bitsCN.com


session.inc.php

网管联盟www.bitsCN.com

 
<?php
//使用mysql存放session 函数表
// by 恋太后天 2005-4-28 网管联盟www.bitsCN.com

if (!isset($include_path)) $include_path = '; 网管网bitsCN_com

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

$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>"); 网管网bitsCN.com

$SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); 54ne.com

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

中国网管联盟www.bitscn.com

function sess_close()
{
    return true;
} 网管网bitsCN_com

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;
} 网管网bitsCN_com

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

    $expiry_time = time() + $SESS_LIFE;

网管联盟www.bitsCN.com

    if ( !get_magic_quotes_gpc() )
    {
        $data = addslashes($data);
    }

网管网bitsCN.com

    $now = time(); 网管网bitsCN_com

    $result = mysql_query("INSERT into `sessions` (`id`, `expiry_time`,  `data`)", $DBsess_link);

中国网管论坛bbs.bitsCN.com

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

中国网管联盟www.bitscn.com

    return $result;
}

中国网管联盟www_bitscn_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;
}

feedom.net

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.bitsCN.com

}

中国网管联盟www_bitscn_com

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

?> 54com.cn


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

网管网bitsCN_com

session_start();

54ne.com

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

feedom.net

?> 网管网bitsCN.com

get_session_test.php 54com.cn


  
<?php
// test for using session
include ("common/Common.config.php");
include ("include/session.inc.php"); 网管网bitsCN_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.bitsCN.com

?> 54ne.com


get_session_test2.php

54ne.com

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

中国网管联盟www.bitscn.com

session_start();
print($_SESSION["c"]);
?>

54ne.com

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
发布者资料
admin 查看详细资料 发送留言 加为好友 用户等级:注册会员 注册时间:2008-05-08 23:05 最后登录:2009-01-05 13:01
推荐内容