sessions.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. require_once "config.php";
  3. require_once "db.php";
  4. define('SESSION_LIFETIME', 365 * 86400);
  5. ini_set("session.name", "epube_sid");
  6. ini_set("session.use_only_cookies", true);
  7. ini_set("session.gc_maxlifetime", SESSION_LIFETIME);
  8. ini_set("session.cookie_lifetime", SESSION_LIFETIME);
  9. if (@$_SERVER['HTTPS'] == "on") {
  10. ini_set("session.cookie_secure", true);
  11. }
  12. session_set_cookie_params(SESSION_LIFETIME);
  13. function s_open ($s, $n) {
  14. return true;
  15. }
  16. function s_read ($id){
  17. $res = Db::get()->query("SELECT data FROM epube_sessions WHERE id='$id'");
  18. if ($line = $res->fetchArray(SQLITE3_ASSOC)) {
  19. return base64_decode($line["data"]);
  20. } else {
  21. $expire = time() + SESSION_LIFETIME;
  22. Db::get()->query("INSERT INTO epube_sessions (id, data, expire)
  23. VALUES ('$id', '', '$expire')");
  24. }
  25. }
  26. function s_write ($id, $data) {
  27. $data = base64_encode($data);
  28. $expire = time() + SESSION_LIFETIME;
  29. Db::get()->query("UPDATE epube_sessions SET data = '$data', expire = '$expire' WHERE id = '$id'");
  30. return true;
  31. }
  32. function s_close () {
  33. return true;
  34. }
  35. function s_destroy($id) {
  36. Db::get()->query("DELETE FROM epube_sessions WHERE id = '$id'");
  37. return true;
  38. }
  39. function s_gc ($expire) {
  40. Db::get()->query("DELETE FROM epube_sessions WHERE expire < " . time());
  41. return true;
  42. }
  43. if (defined('SQLITE_SESSION_STORE') && SQLITE_SESSION_STORE) {
  44. session_set_save_handler("s_open",
  45. "s_close", "s_read", "s_write",
  46. "s_destroy", "s_gc");
  47. } else {
  48. session_save_path(__DIR__ . "/sessions");
  49. }
  50. register_shutdown_function('session_write_close');
  51. session_start();
  52. ?>