Risk 1: If the cookie on the client machine fails due to a virus, the session will be lost.
Hazard 2: sessions are stored in a temporary folder in PHP by default in the form of files. For a small system, this can be done completely,
However, it is not a good solution for a large and frequently accessed system. Suppose there are 1000 people accessing this website each day. A month later, the session Temporary Folder will have 30000 temporary files. Imagine how long it is for a computer to find a session_sid from 30000!
Therefore, to improve efficiency.
Transactions are stored in a database. The specific method is as follows:
1. Change the php. ini file.
Because PHP saves session files by default, we need to change it. That is, change "session. save_handler = Files" to "user ".
Change the session mode to user-defined.
2. Create a database:
Create Table 'db _ Session '(
'Sskey' char (32) not null,
'Expiry' int (11) unsigned not null,
'Value' text not null,
Primary Key ('sskey ')
) Engine = InnoDB default charset = Latin1;
[/Code]
Database: db_session
Column name: sesskey, expiry. Value: sesskey is the primary key.
The value contains the value in the session.
3. Create the session_mysql.php file. This file is used to construct a method to save the session. Modify the parameters and use them directly.
Session_mysql.php
PHP Code :
Copy code The Code is as follows: <? PHP
$ Gb_dbname = "db_mybbs"; // Database Name
$ Gb_dbuser = "root"; // Database User Name
$ Gb_dbpass = "23928484"; // Database Password
$ Gb_dbhostname = "localhost"; // host name or IP address
$ Sess_dbh = "";
$ Sess_life = get_cfg_var ("session. gc_maxlifetime"); // obtain the maximum validity period of the session.
Function sess_open ($ save_path, $ session_name ){
Global $ gb_dbhostname, $ gb_dbname, $ gb_dbuser, $ gb_dbpass, $ sess_dbh;
If (! $ Sess_dbh = mysql_pconnect ($ gb_dbhostname, $ gb_dbuser, $ gb_dbpass )){
Echo "<li> MySQL error:". mysql_error (). "<li> ";
Die ();
}
If (! Mysql_select_db ($ gb_dbname, $ sess_dbh )){
Echo "<li> MySQL error:". mysql_error (). "<li> ";
Die ();
}
Return true;
}
Function sess_close (){
Return true;
}
Function sess_read ($ key ){
Global $ sess_dbh, $ sess_life;
$ Qry = "select value from db_session where sesskey = '$ key' and expiry>". Time ();
$ Qid = mysql_query ($ qry, $ sess_dbh );
If (List ($ value) = mysql_fetch_row ($ qid )){
Return $ value;
}
Return false;
}
Function sess_write ($ key, $ Val ){
Global $ sess_dbh, $ sess_life;
$ Expiry = Time () + $ sess_life;
$ Value = $ val;
$ Qry = "insert into db_session values ('$ key', $ expiry,' $ value ')";
$ Qid = mysql_query ($ qry, $ sess_dbh );
If (! $ Qid ){
$ Qry = "Update db_session set expiry = $ expiry, value = '$ value' where sesskey =' $ key' and expiry>". Time ();
$ Qid = mysql_query ($ qry, $ sess_dbh );
}
Return $ qid;
}
Function sess_destroy ($ key ){
Global $ sess_dbh;
$ Qry = "delete from db_session where sesskey = '$ key '";
$ Qid = mysql_query ($ qry, $ sess_dbh );
Return $ qid;
}
Function sess_gc ($ maxlifetime ){
Global $ sess_dbh;
$ Qry = "delete from db_session where expiry <". Time ();
$ Qid = mysql_query ($ qry, $ sess_dbh );
Return mysql_affected_rows ($ sess_dbh );
}
Session_module_name ();
Session_set_save_handler ("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc ");
?>
4. Create a test file.
The created session_mysql.php file must be referenced before use.
Session_test.php
PHP code:Copy codeThe Code is as follows: <? PHP
Include ("session_mysql.php ");
Session_start ();
$ _ Session ['abc'] = "A: I will be back! ";
$ _ Session ['meto'] = "B: Me too ";
$ _ Session ['name'] = "Louis ";
Echo "<a href = \" get_session_test.php \ "> click me </a> ";
?>
Get_session_test.phpCopy codeThe Code is as follows: <? PHP
Include ("session_mysql.php ");
Session_start ();
Echo $ _ session ['abc'];
Echo "<br> ";
Echo $ _ session ['meto'];
Echo "<br> ";
Echo $ _ session ['name'];
$ _ Session ['wq'] = "12e ";
Echo "<br> <a href = \" get_session_test2.php \ "> Click again </a> ";
?>
Get_session_test2.phpCopy codeThe Code is as follows: <? PHP
Include ("session_mysql.php ");
Session_start ();
Echo $ _ session ['abc'];
Echo "<br> ";
Echo $ _ session ['meto'];
Echo "<br> ";
Echo $ _ session ['name'];
Echo "<br> ";
Echo $ _ session ['wq'];
// Session_destroy (); // The function used to destroy all sessions.
?>