• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mysql存取session实例_PHP

php 搞代码 3年前 (2022-01-25) 23次浏览 已收录 0个评论

files:
common/Common.co¥本文来%源[email protected]搞@^&代*@码)网5搞gaodaima代码nfig.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
(
“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:Can not connect to Mysql server.“);

$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 “click me”;

?>

get_session_test.php



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

session_start();
/*
* http://www.knowsky.com
*/
$_SESSION[“c”] = “
C: I will follow U. ^0^!”;
print($_SESSION[“abc”]);
print(“
“);
print($_SESSION[“meto”]);
echo “
“.
“click again”;

?>


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”]);
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mysql存取session实例_PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址