1
0

session.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Class Session
  4. *
  5. * @property int $id
  6. * @property string $data
  7. * @property string $dts_last_access
  8. */
  9. class Session extends Model {
  10. protected static $table = "session";
  11. protected static $id_field = "id";
  12. protected static $fillable_fields = ["id", "data", "dts_last_access"];
  13. /**
  14. * @param string $name
  15. * @param int $timeout
  16. * @throws Exception
  17. */
  18. public static function start(string $name = "", int $timeout = 3600) {
  19. $handler = new SiteSessionHandler();
  20. self::gc($handler);
  21. session_set_save_handler($handler, true);
  22. session_set_cookie_params($timeout, "/", "");
  23. // Set session name if passed
  24. if(!empty($name)) session_name($name);
  25. session_start();
  26. }
  27. /**
  28. * @param int $timeout
  29. */
  30. public static function extend(int $timeout) {
  31. $id = session_id();
  32. $name = session_name();
  33. setcookie($name, $id, time() + $timeout, "/", "");
  34. }
  35. public static function gc(SessionHandlerInterface $handler) {
  36. try {
  37. if(defined("SESSION_GC_LIFETIME") && defined("SESSION_GC_PROBABILITY")) {
  38. $random_int = rand(0, 999);
  39. if($random_int < SESSION_GC_PROBABILITY) {
  40. $handler->gc(SESSION_GC_LIFETIME);
  41. }
  42. }
  43. }
  44. catch(Exception $e) {
  45. }
  46. }
  47. }