Mysql access session instance

Source: Internet
Author: User
Files: commonCommon. config. phpincludesession. inc. phpsession_test.phpget_session_test.phpget_session_test2.phpCommon.config.php? Php ** Commonconfig * By days ** Databaseconfig * define (DBTYPE, mysql); $ databasearray (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 the day after my fall in love
*/


/*
* 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


// Use mysql to store the session function table
// By: The day after my fall in love

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 'session'
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 'Session' ('id', 'expiry _ time', 'data')", $ DBsess_link );

If (! $ Result)
{
$ Result = mysql_query ("UPDATE 'session' 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 'session' 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 will 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 will 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"]);
?>

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.