By default, PHP sessions are saved in files. We can see a line like this in the PHP configuration file php.ini, session.save_handler="files", which means that files are used to save sessions. Yes, if we want to use the database to save, we need to change it to the support mode and rename it 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 Create database tables.
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 according to your needs. MySQL database, and 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:
Copy code The code is as follows:
CREATE DATABASE 'session'; Create table structure CREATE TABLE 'session'( id CHAR(30) NOT NULL , 'user 'CHAR(30), data CHAR(3000) ,PARMIRY BY ('id') );
Let’s write the file session_start.php to save the session
Copy the code The code is as follows:
< ?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 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
?>
Now our work has been completed, as long as you need to use it During the session, put session_user_start.php. Include it,
Note , this file must be included in the first line of the file, and then you can use it in the same way as the session of the file.
http://www.bkjia.com/PHPjc/327508.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327508.htmlTechArticleBy default, php session is saved in the file mode. We use the php configuration file php. You can see a line like this in ini, session.save_handler="files", which means...