A detailed introduction to PHP session Storage _php Tutorial

Source: Internet
Author: User
Tags php session sesion
PHP provides three ways to store sesion: File/Memory/custom storage, which by default is used for file storage.It is not appropriate to use this approach on heavily visited sites, as this can result in a large amount of redundancy in the input and output.
Here is a session storage method based on MySQL database.
the first configuration to be performed is as follows:
1>php.ini Session.save_handler = files in the files changed to user, the other default, restart Apache (as if not changed)
2> the database created by this instance is called PHP, Username:root password:root
The table structure of the database designed by this example is as follows:
Copy CodeThe code is as follows:
CREATE TABLE MySession (
Session_key char (+) NOT NULL,
Session_data text,
Session_expiry Int (11),
Primary KEY (Session_key)
);

The first column represents the store session ID, the second column stores the data in the session, and the third column stores the expiration date, hehe (the table structure is so simple)
The following is the key to the implementation of the custom function Session_set_save_handler (...)
Copy CodeThe code is as follows:
function Mysession_open ($save _path, $session _name)
{
@mysql_connect ("localhost", "root", "root")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or is not available");
return true;
}
function Mysession_close ()
{
return true;
}
function Mysession_read ($key)
{
@mysql_connect ("localhost", "root", "root")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time (); Get session Expiration Time
Execute SQL statement to get the value of session
$query = @mysql_query ("Select Session_data from MySession"
." where Session_key = ' $key ' and Session_expiry > $expiry _time ")
Or Die ("SQL statement execution failed");
if ($row = mysql_fetch_array ($query))
return $row [' Session_data '];
Else
return false;
}
function Mysession_write ($key, $data)
{
@mysql_connect ("localhost", "root", "root")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time () + 1200; Get session Expiration Time
Query whether the key value of session already exists
$query = @mysql_query ("Select Session_data from MySession"
." where Session_key = ' $key ' ")
Or Die ("SQL statement execution failed");
If it does not exist, the insert operation is performed, or the update operation is performed
if (mysql_numrows ($query) = = 0)
{
Execute SQL statement Insert value of Session
$query = @mysql_query ("INSERT into mysession values (' $key ', ' $data ', $expiry _time)")
Or Die ("SQL statement execution failed");
}
Else
{
Execute SQL statement to update the value of Session
$query = @mysql_query ("Update mysession set"
." Session_data = ' $data ', Session_expiry = $expiry _time "
." where Session_key = ' $key ' ")
Or Die ("SQL statement execution failed");
}
return $query;
}
function Mysession_destroy ($key)
{
@mysql_connect ("localhost", "root", "root")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or is not available");
Execute SQL statement Delete session
$query = @mysql_query ("Delete from mysession where Session_key = ' $key '")
Or Die ("SQL statement execution failed");
return $query;
}
function mysession_gc ($expiry _time)
{
@mysql_connect ("localhost", "root", "root")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("PHP")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time ();
Execute SQL statement Delete session
$query = @mysql_query ("Delete from mysession where Session_expiry < $expiry _time")
Or Die ("SQL statement execution failed");
return $query;
}
Set up user-defined session store
Session_set_save_handler (' Mysession_open ',
' Mysession_close ',
' Mysession_read ',
' Mysession_write ',
' Mysession_destroy ',
' mysession_gc ');
?>

The last side is the test code.
1> Storage Page save.php
Copy CodeThe code is as follows:
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
$_session[' username ' = "Simon";
$_session[' password '] = "123456";
?>

2. Display Page show.php
Copy CodeThe code is as follows:
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
echo "UserName:". $_session[' UserName ']. "
";
echo "PassWord:". $_session[' PassWord ']. "
";
?>

Add a little experience: there is an inexplicable re-storage session when the page reported this error
Failed to initialize storage module
Later found the relevant information to know is my session storage database file storage, and Session_Start (), my database has not woken up ...

http://www.bkjia.com/PHPjc/327846.html www.bkjia.com true http://www.bkjia.com/PHPjc/327846.html techarticle PHP provides three ways to store sesion: File/Memory/custom storage, which by default is used for file storage. This is not a good way to do this on a website with a large number of visits, as this will lead to ...

  • 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.