file.php 903 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php namespace System\Session;
  2. class File implements Driver, Sweeper {
  3. /**
  4. * Load a session by ID.
  5. *
  6. * @param string $id
  7. * @return array
  8. */
  9. public function load($id)
  10. {
  11. if (file_exists($path = SESSION_PATH.$id)) return unserialize(file_get_contents($path));
  12. }
  13. /**
  14. * Save a session.
  15. *
  16. * @param array $session
  17. * @return void
  18. */
  19. public function save($session)
  20. {
  21. file_put_contents(SESSION_PATH.$session['id'], serialize($session), LOCK_EX);
  22. }
  23. /**
  24. * Delete a session by ID.
  25. *
  26. * @param string $id
  27. * @return void
  28. */
  29. public function delete($id)
  30. {
  31. @unlink(SESSION_PATH.$id);
  32. }
  33. /**
  34. * Delete all expired sessions.
  35. *
  36. * @param int $expiration
  37. * @return void
  38. */
  39. public function sweep($expiration)
  40. {
  41. foreach (glob(SESSION_PATH.'*') as $file)
  42. {
  43. if (filetype($file) == 'file' and filemtime($file) < $expiration) @unlink($file);
  44. }
  45. }
  46. }