MySQL Access session Instance _php tutorial

Source: Internet
Author: User

Files
common/common.config.php
include/session.inc.php
session_test.php
get_session_test.php
get_session_test2.php


Common.config.php

/*
* Common Config
* By Love too the day after tomorrow
*/


/*
* Database Config
*/
Define ("DBTYPE", "MySQL");
$database = array
(
"MySQL" = array
(
"Default" = = Array
(
"Host" = "localhost",
"User" = "root",
"Password" and "" ",
"DBName" = "" "
),
"Session" = = array
(
"Host" = "localhost",
"User" = "session",
"Password" = "Session",
"DBName" = "Sessions"
)
)
);

?>


session.inc.php


Using MySQL to store the session function table
By love too after 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 does 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

Test for using session
Include ("common/common.config.php");
Include ("include/session.inc.php");

Session_Start ();

$_session["abc"] = "a:i'll be back!";
$_session["Meto"] = "b:me too";
echo "Click Me";

?>

get_session_test.php



Test for using session
Include ("common/common.config.php");
Include ("include/session.inc.php");

Session_Start ();
/*
* www.knowsky.com
*/
$_session["C"] = "
C:i would follow U. ^0^! ";
Print ($_session["abc"]);
Print ("
");
Print ($_session["Meto"]);
echo "
".
"Click again";

?>


get_session_test2.php


get_session_test2.php
Test for using session
Include ("common/common.config.php");
Include ("include/session.inc.php");

Session_Start ();
Print ($_session["C"]);
?>

http://www.bkjia.com/PHPjc/508383.html www.bkjia.com true http://www.bkjia.com/PHPjc/508383.html techarticle 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 love too day/...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.