Home > Backend Development > PHP Tutorial > PHP uses database to save session_PHP tutorial

PHP uses database to save session_PHP tutorial

WBOY
Release: 2016-07-13 17:35:15
Original
1104 people have browsed it

PHP saves sessions by default in the form of files. This can only be used on Windows where the file space overhead is very small, but if we use the file system on uinx or liux At that time, the file space overhead of such a file system was very large. However, sessions were used all the time. A large number of users would create many session files, which would bring performance problems to the entire server. On the other hand, On the other hand, if the server adopts a cluster method, the consistency of the session cannot be maintained, so we are ready to use a database to save the session. In this way, no matter how many servers are used at the same time, they only need to save their sessions in one database. The complete session can be saved on the server. Please read on for details on how to implement it.

By default, PHP sessions are saved in the file format. We can see a line like this in the PHP configuration file php.ini, session.save_handler="files", which means using files. To save the session, if we want to use the database to save it, we need to change it to support mode and rename it to session.save_handler="use". However, this only means that we do not use files to store sessions. We also need to Select the database and the tables to create the database.
To establish the database and the table structure of the database, we can use any database that PHP can use. Because the combination of PHP and MySQL is the best, I will use MySQL for the example. Of course, you can change the name to another database according to your needs. At the same time, because MySQL does not have transaction functions, it is faster than other databases. However, saving session files does not require transaction processing, and I decided to do it better.
Create database , CREATE DATABASE session; Create table structure CREATE TABLE session( id CHAR(30) NOT NULL , user CHAR(30), data CHAR(3000) ,PARMIRY BY (id) );
Write php file
http://www.knowsky.com

$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 ))
                                                                                                                                                                                                        ; 
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 will 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 it as session_user_start.php.

Now our work has been completed, as long as you need to use session, change session_user_start.php. Include it. Note that this file must be included in the first line of the file. Then you can use it in the same way as the session of the file.

This article was written in a hurry. If there is something wrong, please give me your valuable opinions. Your correction is welcome.

aspx">http://blog.csdn.net/eoe2005/archive/2007/02/20/1512131.aspx

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508370.htmlTechArticlephp saves the session by default in the form of a file, which only consumes very little space in the file. It can be used on Windows, but if we use uinx or li...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template