file.php 922 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php namespace System\Session;
  2. class File implements Driver {
  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))
  12. {
  13. return unserialize(file_get_contents($path));
  14. }
  15. }
  16. /**
  17. * Save a session.
  18. *
  19. * @param array $session
  20. * @return void
  21. */
  22. public function save($session)
  23. {
  24. file_put_contents(SESSION_PATH.$session['id'], serialize($session), LOCK_EX);
  25. }
  26. /**
  27. * Delete a session by ID.
  28. *
  29. * @param string $id
  30. * @return void
  31. */
  32. public function delete($id)
  33. {
  34. @unlink(SESSION_PATH.$id);
  35. }
  36. /**
  37. * Delete all expired sessions.
  38. *
  39. * @param int $expiration
  40. * @return void
  41. */
  42. public function sweep($expiration)
  43. {
  44. foreach (glob(SESSION_PATH.'*') as $file)
  45. {
  46. if (filetype($file) == 'file' and filemtime($file) < $expiration)
  47. {
  48. @unlink($file);
  49. }
  50. }
  51. }
  52. }