cookie.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php namespace Laravel\Session\Drivers;
  2. use Laravel\Security\Crypter;
  3. class Cookie implements Driver {
  4. /**
  5. * The crypter instance.
  6. *
  7. * All session cookies have an encrypted payload. Since the session contains sensitive
  8. * data that cannot be compromised, it is important that the payload be encrypted using
  9. * the strong encryption provided by the Crypter class.
  10. *
  11. * @var Crypter
  12. */
  13. private $crypter;
  14. /**
  15. * The cookie manager instance.
  16. *
  17. * @var Cookie
  18. */
  19. private $cookies;
  20. /**
  21. * Create a new Cookie session driver instance.
  22. *
  23. * @param Crypter $crypter
  24. * @param Cookie $cookies
  25. * @return void
  26. */
  27. public function __construct(Crypter $crypter, \Laravel\Cookie $cookies)
  28. {
  29. $this->crypter = $crypter;
  30. $this->cookies = $cookies;
  31. }
  32. /**
  33. * Load a session from storage by a given ID.
  34. *
  35. * If no session is found for the ID, null will be returned.
  36. *
  37. * @param string $id
  38. * @return array
  39. */
  40. public function load($id)
  41. {
  42. if ($this->cookies->has('session_payload'))
  43. {
  44. return unserialize($this->crypter->decrypt($this->cookies->get('session_payload')));
  45. }
  46. }
  47. /**
  48. * Save a given session to storage.
  49. *
  50. * @param array $session
  51. * @param array $config
  52. * @return void
  53. */
  54. public function save($session, $config)
  55. {
  56. extract($config);
  57. $payload = $this->crypter->encrypt(serialize($session));
  58. $this->cookies->put('session_payload', $payload, $lifetime, $path, $domain);
  59. }
  60. /**
  61. * Delete a session from storage by a given ID.
  62. *
  63. * @param string $id
  64. * @return void
  65. */
  66. public function delete($id)
  67. {
  68. $this->cookies->forget('session_payload');
  69. }
  70. }