PHP saves the session by default is the way to save the file, which is only available on Windows with a small amount of space on the file, but if we adopt Uinx or Liux file system, the file space overhead of such file system is very large, However, the session is to be used all the times, a large number of users will create a lot of session files, so that the entire server brings performance problems.
On the other hand, if the server is clustered, it cannot maintain session consistency, 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, Just save their session on a database server to save the session complete, how to implement please continue to see.
PHP Save session By default is the use of the file method to save, we in the PHP configuration file php.ini can see such a line, session.save_handler= "files", this means to use the file to save the session , to use the database to save, we need to modify the user mode, renamed session.save_handler= "use" on it, but this is just to show that we do not file the way to store the session, we also have to select the database and build the database table.
To build the database and database table structure, we can use any PHP can be used by the database, because the combination of PHP and MySQL is best, I use MySQL to do the case, of course, according to your needs can be renamed to other databases, and because MySQL has no function of things, It's faster than other databases, but saving the session doesn't require anything, and I think it's better here.
Create database ' session '; Create a table structure of ' session ' (ID char () NOT NULL, ' user ' char (+), data char (+), parmiry by (' id '));
PHP Save session Write PHP file
- < ? 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;
- }
- }
- Function GC ($MAXLIFETIME)
- {
- return true;
- }
- Session_set_save_handler ("Open",
"Close", "read", "write", "Destroy", "GC");
- Session_Start ();
- Proceed to use sessions normally
- ?>
Save As Session_user_start. Php.
Now the work of our PHP save session has been completed, 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 used as a method of using the file's session.
http://www.bkjia.com/PHPjc/446131.html www.bkjia.com true http://www.bkjia.com/PHPjc/446131.html techarticle PHP Save session By default is the way to save the file, which is only available on Windows with a small space overhead, but if we use Uinx or Liu ...