セッションがデータベースに保存されました

WBOY
リリース: 2016-07-25 09:09:01
オリジナル
950 人が閲覧しました
  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 セッション SET update_time =? 、日付 = ? 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. 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. 新しい SessionToDB($pdo);
  85. } catch(PDOException $e) {
  86. echo 'Error: '.$e->getMessage();
  87. }
复制代码


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!