Example of using mysql database to store session in php

Source: Internet
Author: User
Example of using mysql database to store session in php

  1. Create table 'DB _ session '(
  2. 'Sskey' char (32) not null,
  3. 'Expiry' int (11) unsigned not null,
  4. 'Value' text not null,
  5. Primary key ('sskey ')
  6. ) 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:

  1. $ Gb_DBname = "db_myBBS"; // database name
  2. $ Gb_DBuser = "root"; // database user name
  3. $ Gb_DBpass = "23928484"; // database password
  4. $ Gb_DBHOSTname = "localhost"; // host name or IP address
  5. $ SESS_DBH = "";
  6. $ SESS_LIFE = get_cfg_var ("session. gc_maxlifetime"); // obtain the maximum validity period of the session.
  7. Function sess_open ($ save_path, $ session_name ){
  8. Global $ gb_DBHOSTname, $ gb_DBname, $ gb_DBuser, $ gb_DBpass, $ SESS_DBH;
  9. If (! $ SESS_DBH = mysql_pconnect ($ gb_DBHOSTname, $ gb_DBuser, $ gb_DBpass )){
  10. Echo"
  11. MySql Error: ". mysql_error ()."
  12. ";
  13. Die ();
  14. }
  15. If (! Mysql_select_db ($ gb_DBname, $ SESS_DBH )){
  16. Echo"
  17. MySql Error: ". mysql_error ()."
  18. ";
  19. Die ();
  20. }
  21. Return true;
  22. }
  23. Function sess_close (){
  24. Return true;
  25. }
  26. Function sess_read ($ key ){
  27. Global $ SESS_DBH, $ SESS_LIFE;
  28. $ Qry = "select value from db_session where sesskey = '$ key' and expiry>". time ();
  29. $ Qid = mysql_query ($ qry, $ SESS_DBH );
  30. If (list ($ value) = mysql_fetch_row ($ qid )){
  31. Return $ value;
  32. }
  33. Return false;
  34. }
  35. Function sess_write ($ key, $ val ){
  36. Global $ SESS_DBH, $ SESS_LIFE;
  37. $ Expiry = time () + $ SESS_LIFE;
  38. $ Value = $ val;
  39. $ Qry = "insert into db_session values ('$ key', $ expiry,' $ value ')";
  40. $ Qid = mysql_query ($ qry, $ SESS_DBH );
  41. If (! $ Qid ){
  42. $ Qry = "update db_session set expiry = $ expiry, value = '$ value' where sesskey =' $ key' and expiry>". time ();
  43. $ Qid = mysql_query ($ qry, $ SESS_DBH );
  44. }
  45. Return $ qid;
  46. }
  47. Function sess_destroy ($ key ){
  48. Global $ SESS_DBH;
  49. $ Qry = "delete from db_session where sesskey = '$ key '";
  50. $ Qid = mysql_query ($ qry, $ SESS_DBH );
  51. Return $ qid;
  52. }
  53. Function sess_gc ($ maxlifetime ){
  54. Global $ SESS_DBH;
  55. $ Qry = "delete from db_session where expiry <". time ();
  56. $ Qid = mysql_query ($ qry, $ SESS_DBH );
  57. Return mysql_affected_rows ($ SESS_DBH );
  58. }
  59. Session_module_name ();
  60. Session_set_save_handler ("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc ");
  61. ?>

4. create a test file. The created session_mysql.php file must be referenced before use. File: session_test.php

  1. Include ("session_mysql.php ");
  2. Session_start ();
  3. $ _ SESSION ['ABC'] = "A: I will be back! ";
  4. $ _ SESSION ['meto'] = "B: Me too ";
  5. $ _ SESSION ['name'] = "louis ";
  6. Echo "click me ";
  7. ?>

File: get_session_test.php

  1. Include ("session_mysql.php ");
  2. Session_start ();
  3. Echo $ _ SESSION ['ABC'];
  4. Echo"
    ";
  5. Echo $ _ SESSION ['meto'];
  6. Echo"
    ";
  7. Echo $ _ SESSION ['name'];
  8. $ _ SESSION ['wq'] = "12e ";
  9. Echo"
    Click again ";
  10. ?>

File: get_session_test2.php

  1. Include ("session_mysql.php ");
  2. Session_start ();
  3. Echo $ _ SESSION ['ABC'];
  4. Echo"
    ";
  5. Echo $ _ SESSION ['meto'];
  6. Echo"
    ";
  7. Echo $ _ SESSION ['name'];
  8. Echo"
    ";
  9. Echo $ _ SESSION ['wq'];
  10. // Session_destroy (); // The function used to destroy all sessions.
  11. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.