Mysql storage PHPSession

Source: Internet
Author: User
The first configuration is as follows: change the files in sessionsave_handlerfiles to User in 1phpini, and restart Apache2 by default.

The first configuration is as follows:

1. change the files in session. save_handler = files to User in php. ini. restart Apache by default.

2. the name of the database created in this instance is php, username: root password: root.

The table structure of the database designed for this instance is as follows:

  1. Create table mysession (
  2. Session_key char (32) not null,
  3. Session_data text,
  4. Session_expiry int (11 ),
  5. Primary key (session_key)
  6. );

The first column indicates the session ID, the second column stores the data in the session, and the third column stores the validity period. The following is the key custom function implementation session_set_save_handler (......), the code is as follows:

User-define-session-inc.php

  1. Function mysession_open ($ save_path, $ session_name)
  2. {
  3. @ Mysql_connect ("localhost", "root", "root") or die ("database server connection failed ");
  4. @ Mysql_select_db ("php") or die ("The database does not exist or is unavailable ");
  5. Return true;
  6. }
  7. Function mysession_close ()
  8. {
  9. Return true;
  10. }
  11. Function mysession_read ($ key)
  12. {
  13. @ Mysql_connect ("localhost", "root", "root") or die ("database server connection failed ");
  14. @ Mysql_select_db ("php") or die ("The database does not exist or is unavailable ");
  15. $ Expiry_time = time ();
  16. $ Query = @ mysql_query ("select session_data from mysession where session_key = '$ key' and session_expiry> $ expiry_time") or die ("SQL statement execution failed ");
  17. If ($ row = mysql_fetch_array ($ query ))
  18. Return $ row ['session _ data'];
  19. Else
  20. Return false;
  21. }
  22. Function mysession_write ($ key, $ data)
  23. {
  24. @ Mysql_connect ("localhost", "root", "root") or die ("database server connection failed ");
  25. @ Mysql_select_db ("php") or die ("The database does not exist or is unavailable ");
  26. $ Expiry_time = time () + 1200;
  27. $ Query = @ mysql_query ("select session_data from mysession where session_key = '$ key'") or die ("SQL statement execution failed ");
  28. If (mysql_numrows ($ query) = 0)
  29. {
  30. $ Query = @ mysql_query ("insert into mysession values ('$ key',' $ data', $ expiry_time)") or die ("SQL statement execution failed ");
  31. }
  32. Else
  33. {
  34. $ Query = @ mysql_query ("update mysession set session_data = '$ data', session_expiry = $ expiry_time where session_key =' $ key '") or die ("SQL statement execution failed ");
  35. }
  36. Return $ query;
  37. }
  38. Function mysession_destroy ($ key)
  39. {
  40. @ Mysql_connect ("localhost", "root", "root") or die ("database server connection failed ");
  41. @ Mysql_select_db ("php") or die ("The database does not exist or is unavailable ");
  42. $ Query = @ mysql_query ("delete from mysession where session_key = '$ key '")
  43. Or die ("SQL statement execution failed ");
  44. Return $ query;
  45. }
  46. Function mysession_gc ($ expiry_time)
  47. {
  48. @ Mysql_connect ("localhost", "root", "root") or die ("database server connection failed ");
  49. @ Mysql_select_db ("php") or die ("The database does not exist or is unavailable ");
  50. $ Expiry_time = time ();
  51. $ Query = @ mysql_query ("delete from mysession where session_expiry <$ expiry_time") or die ("SQL statement execution failed ");
  52. Return $ query;
  53. }
  54. Session_set_save_handler ('mysession _ open', 'mysession _ close', 'mysession _ read', 'mysession _ write', 'mysession _ destroy ', 'mysession _ gc ');
  55. ?>

The test code is as follows:

1. save. php

  1. Include ('User-define-session-inc.php ');
  2. Session_start ();
  3. $ _ SESSION ['username'] = "Simon ";
  4. $ _ SESSION ['password'] = "123456 ";
  5. ?>

2. show. php

  1. Include ('User-define-session-inc.php ');
  2. Session_start ();
  3. Echo "UserName:". $ _ SESSION ['username']."
  4. ";
  5. Echo "PassWord:". $ _ SESSION ['password']."
  6. ";
  7. ?>

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.