首 页 教育新闻课件中心论文中心教学教案试题中心语文专题综合下载技术教程公务员  
设为首页
加入收藏
联系我们
您当前的位置:中国教育资源网 -> 技术教程 -> 网站建设 -> 源码精华 -> 技术内容 退出登录 用户管理

使用MYSQL存取session实例源码精华教程

论文作者:佚名  论文来源:不详  论文发布时间:2006-6-20 2:38:47  论文发布人:chjchjchj

减小字体 增大字体

Quoted from Unkown:files:
common/Common.config.php
include/session.inc.php
session_test.php
get_session_test.php
get_session_test2.phpCommon.config.php

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


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

?>

session.inc.php

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

if (!isset($include_path)) $include_path = '';

if (!is_array($database))
{
include ($include_path."common/Common.config.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");

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

function sess_close()
{
return true;
}

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;
}

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

$expiry_time = time() + $SESS_LIFE;

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

$now = time();

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

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

return $result;
}

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

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);

}

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

?>

session_test.php

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

session_start();

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

?>

get_session_test.php

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

session_start();

$_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>";

?>

get_session_test2.php

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

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

(出处:

[] [返回上一页] [打 印] [收 藏]  
 ∷相关技术评论  (评论内容只代表网友观点,与本站立场无关!) [查看发表评论...]
 
 中国教育资源网免费技术教程下载中心-站内广告 站内广告 中国教育资源网免费技术教程下载中心-站内广告 
 中国教育资源网站内搜索 站内搜索 中国教育资源网站内搜索 
 

   
 中国教育资源网免费技术教程下载中心-栏目导航 栏目导航 中国教育资源网免费技术教程下载中心-栏目导航 
· Dreamweaver · FrontPage
· CSSHTML · DOMJS
· Google排名 · 搜索研究
· 网络赚钱 · Alexa相关
· 建站交流 · 源码精华
· 经验技巧
 
中国教育资源网免费技术教程下载中心-相关教程  相关技术 中国教育资源网免费技术教程下载中心-相关教程
· Mysql中左连接的使用
· 使用MySql ODBC进行
· 怎样在vc、delphi中
· 在服务器上安装、使
 中国教育资源网免费技术教程下载中心-本月热门教程 本月热门 中国教育资源网免费技术教程下载中心-本月热门教程 
 
 中国教育资源网免费技术教程下载中心-本日热门论文 本日热门 中国教育资源网免费技术教程下载中心-本日热门论文 
 
关于本站 - 网站帮助 - 免费课件 - 美容 - 绿色软件 - 软件下载 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 网站留言
浙ICP备06010405号 Email:cnkjz@163.com 技术支持:名流设计
版权所有 Copyright© 2002-2004 名流