- 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;
-
The database name is db_session column name: sesskey, expiry, where: 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.phpphp code:
- $ 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"
- MySql Error: ". mysql_error ()."
- ";
- Die ();
- }
- If (! Mysql_select_db ($ gb_DBname, $ SESS_DBH )){
- Echo"
- MySql Error: ". mysql_error ()."
- ";
- 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. File: session_test.php
- Include ("session_mysql.php ");
- Session_start ();
- $ _ SESSION ['ABC'] = "A: I will be back! ";
- $ _ SESSION ['meto'] = "B: Me too ";
- $ _ SESSION ['name'] = "louis ";
- Echo "click me ";
- ?>
File: get_session_test.php
- Include ("session_mysql.php ");
- Session_start ();
- Echo $ _ SESSION ['ABC'];
- Echo"
";
- Echo $ _ SESSION ['meto'];
- Echo"
";
- Echo $ _ SESSION ['name'];
- $ _ SESSION ['wq'] = "12e ";
- Echo"
Click again ";
- ?>
File: get_session_test2.php
- Include ("session_mysql.php ");
- Session_start ();
- Echo $ _ SESSION ['ABC'];
- Echo"
";
- Echo $ _ SESSION ['meto'];
- Echo"
";
- Echo $ _ SESSION ['name'];
- Echo"
";
- Echo $ _ SESSION ['wq'];
- // Session_destroy (); // The function used to destroy all sessions.
- ?>
|