cookie.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php namespace Laravel\Session;
  2. use Laravel\Config;
  3. use Laravel\Security\Crypter;
  4. class Cookie extends Driver {
  5. /**
  6. * The cookie engine instance.
  7. *
  8. * @var Cookie_Engine
  9. */
  10. private $cookie;
  11. /**
  12. * The Crypter instance.
  13. *
  14. * @var Crypter
  15. */
  16. private $crypter;
  17. /**
  18. * Create a new Cookie session driver instance.
  19. *
  20. * @param Crypter $crypter
  21. * @param Cookie $cookie
  22. * @return void
  23. */
  24. public function __construct(Crypter $crypter, Cookie $cookie)
  25. {
  26. $this->cookie = $cookie;
  27. $this->crypter = $crypter;
  28. if (Config::get('application.key') == '')
  29. {
  30. throw new \Exception("You must set an application key before using the Cookie session driver.");
  31. }
  32. }
  33. /**
  34. * Load a session by ID.
  35. *
  36. * The session will be retrieved from persistant storage and returned as an array.
  37. * The array contains the session ID, last activity UNIX timestamp, and session data.
  38. *
  39. * @param string $id
  40. * @return array
  41. */
  42. protected function load($id)
  43. {
  44. if ($this->cookie->has('session_payload'))
  45. {
  46. return unserialize($this->crypter->decrypt($this->cookie->get('session_payload')));
  47. }
  48. }
  49. /**
  50. * Save the session to persistant storage.
  51. *
  52. * @return void
  53. */
  54. protected function save()
  55. {
  56. if ( ! headers_sent())
  57. {
  58. extract(Config::get('session'));
  59. $payload = $this->crypter->encrypt(serialize($this->session));
  60. $this->cookie->put('session_payload', $payload, $lifetime, $path, $domain, $https, $http_only);
  61. }
  62. }
  63. /**
  64. * Delete the session from persistant storage.
  65. *
  66. * @return void
  67. */
  68. protected function delete()
  69. {
  70. $this->cookie->forget('session_payload');
  71. }
  72. }