PHP saves session with database

Source: Internet
Author: User
Tags file system garbage collection mysql php session php and php and mysql sessions create database

PHP saves the session by default in the way the file is saved, which is only available on Windows with a very small space overhead, but if we use Uinx or the file system on Liux, the file space overhead of such a file system is very large, However, the session is to be used every moment, a large number of users drunken create a lot of session files, which brings performance problems to the entire server, on the other hand, if the server to use the way of clustering can not maintain the consistency of sessions, So we are ready to use the database to save the session, so that no matter how many servers are used at the same time, as long as their session is saved on a database server can be saved the complete sessions, how to achieve, please continue to see.

PHP Session By default is the file way to save, we in the PHP configuration file php.ini can see such a line, session.save_handler= "files", this means that the use of documents to save the session , to use the database to save, we need to modify into a support mode, renamed session.save_handler= "use" on it, but, this is only to show that we do not use the file to store the session, we also have to select the database and database tables.

Build database and database table structure, we can use any database PHP can be used, because the combination of PHP and MySQL is the best, I use MySQL to do the case, of course, according to your needs can be renamed other databases, and because MySQL does not have the function of things, This is also faster than the other database, but save the session book, one does not have to deal with things, in addition, I definitely better.

Create database ' session '; Creating table structure CREATE TABLE ' session ' (id char (= not NULL, ' user ' char (), Data CHAR (3000), parmiry by (' id '));
Writing PHP files

<?php
$con =mysql_connection ("127.0.0.1", "User", "pass");
mysql_select_db ("session");
function open ($save _path, $session _name)
{
return (true);
}
function Close ()
{
return (true);
}
function Read ($id)
{
if ($result = mysql_query ("SELECT * from session WHERE id= ' $id '))
{
if ($row = Mysql_felth_row ($result))
{return $row ["Data]";}
}
Else
{
Return "";
}
}
function Write ($id, $sess _data)
{
if ($result = mysql_query ("UPDATE session SET data= ' $sess _data ' WHERE id= ' $id '))
{
return true;
}
Else
{
return false;
}
}
function Destroy ($ID)
{
if ($result = mysql_query ("DELETE * from session WHERE id= ' $id '))
{
return true;
}
Else
{
return false;
}
}
/*********************************************
* Warning-you'll need to implement some *
* Sort of garbage collection routine here. *
*********************************************/
Function GC ($MAXLIFETIME)
{
return true;
}
Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");
Session_Start ();
Proceed to use sessions normally
?>

Save to become session_user_start.php.

Now our work is done, as long as you need to use the session, the session_user_start.php. Included, note that this file must be included in the first line of the file, and then use the old one to use the same method as the file session.

This article is written in haste, if there are any wrong places please give your valuable advice. Welcome to your treatise.



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.