Session Save to Database

Source: Internet
Author: User
<无详细内容>
  1. Class Sessiontodb
  2. {
  3. Private $_path = null;
  4. Private $_name = null;
  5. Private $_pdo = null;
  6. Private $_ip = null;
  7. Private $_maxlifetime = 0;
  8. Public function __construct (PDO $pdo)
  9. {
  10. Session_set_save_handler (
  11. Array (& $this, ' Open '),
  12. Array (& $this, ' Close '),
  13. Array (& $this, ' read '),
  14. Array (& $this, ' write '),
  15. Array (& $this, ' Destroy '),
  16. Array (& $this, ' GC ')
  17. );
  18. $this->_pdo = $pdo;
  19. $this->_ip =!empty ($_server[' remote_addr ')? $_server[' REMOTE_ADDR ': null;
  20. $this->_maxlifetime = ini_get (' session.gc_maxlifetime ');
  21. }
  22. Public function open ($path, $name)
  23. {
  24. return true;
  25. }
  26. Public Function Close ()
  27. {
  28. return true;
  29. }
  30. Public function read ($id)
  31. {
  32. $sql = ' SELECT * from session where PHPSESSID =? ';
  33. $stmt = $this->_pdo->prepare ($sql);
  34. $stmt->execute (Array ($id));
  35. if (! $result = $stmt->fetch (PDO::FETCH_ASSOC)) {
  36. return null;
  37. } elseif ($this->_ip! = $result [' client_ip ']) {
  38. return null;
  39. } elseif ($result [' Update_time ']+ $this->_maxlifetime < Time ()} {
  40. $this->destroy ($id);
  41. return null;
  42. } else {
  43. return $result [' data '];
  44. }
  45. }
  46. Public function Write ($id, $data)
  47. {
  48. $sql = ' SELECT * from session where PHPSESSID =? ';
  49. $stmt = $this->_pdo->prepare ($sql);
  50. $stmt->execute (Array ($id));
  51. if ($result = $stmt->fetch (PDO::FETCH_ASSOC)) {
  52. if ($result [' data ']! = $data) {
  53. $sql = ' UPDATE session SET Update_time =? , date =? WHERE PHPSESSID =? ';
  54. $stmt = $this->_pdo->prepare ($sql);
  55. $stmt->execute (Array (Time (), $data, $id));
  56. }
  57. } else {
  58. if (!empty ($data)) {
  59. $sql = ' INSERT into session (PHPSESSID, Update_time, CLIENT_IP, data) VALUES (?,?,?,?) ';
  60. $stmt = $this->_pdo->prepare ($sql);
  61. $stmt->execute (Array ($id, Time (), $this->_ip, $data));
  62. }
  63. }
  64. return true;
  65. }
  66. Public function Destroy ($ID)
  67. {
  68. $sql = ' DELETE from session WHERE PHPSESSID =? ';
  69. $stmt = $this->_pdo->prepare ($sql);
  70. $stmt->execute (Array ($id));
  71. return true;
  72. }
  73. Public Function GC ($MAXLIFETIME)
  74. {
  75. $sql = ' DELETE from session WHERE Update_time <? ';
  76. $stmt = $this->_pdo->prepare ($sql);
  77. $stmt->execute (Array (Time ()-$maxLifeTime));
  78. return true;
  79. }
  80. }
  81. try{
  82. $pdo = new PDO (' Mysql:host=localhost;dbname=rphp4zf ', ' root ', ' Rickyfeng ');
  83. $pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);
  84. New Sessiontodb ($PDO);
  85. } catch (Pdoexception $e) {
  86. Echo ' Error: '. $e->getmessage ();
  87. }
Copy Code
  • 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.