cookie.php 1.6 KB

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