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 Jiuyao create a lot of session files, so that the entire server to bring performance problems, on the other hand, if the server cluster-based approach can not maintain the consistency of the session, So we are ready to use a database to save the session, so that no matter how many servers are used at the same time, just put their session on a database server can save the session of the complete, how to implement please continue to see.
PHP 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 that the use of files to save the session , to use the database to save, we need to change into a support model, 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 the database to build the 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, This is also faster than other databases, but save the session book, one not to deal with things, and in addition I decided better.
Create DATABASE session; Creating table structure CREATE TABLE session (ID char () not NULL, user char (+), data char (+), parmiry by (ID));
Writing PHP files
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))
{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
?> saved as session_user_start.php.
Now our work is finished, 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 can in the same way that you want to use the file's session.
This article is written in haste, if there is a wrong place please make your valuable comments. Welcome to your treatise.
aspx ">http://blog.csdn.net/eoe2005/archive/2007/02/20/1512131.aspx