cookie.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php namespace Laravel;
  2. class Cookie {
  3. /**
  4. * All of the cookies for the current request.
  5. *
  6. * @var array
  7. */
  8. private $cookies;
  9. /**
  10. * Create a new cookie manager instance.
  11. *
  12. * @param array $cookies
  13. * @return void
  14. */
  15. public function __construct(&$cookies)
  16. {
  17. $this->cookies = &$cookies;
  18. }
  19. /**
  20. * Determine if a cookie exists.
  21. *
  22. * @param string $name
  23. * @return bool
  24. */
  25. public function has($name)
  26. {
  27. return ! is_null($this->get($name));
  28. }
  29. /**
  30. * Get the value of a cookie.
  31. *
  32. * @param string $name
  33. * @param mixed $default
  34. * @return string
  35. */
  36. public function get($name, $default = null)
  37. {
  38. return Arr::get($this->cookies, $name, $default);
  39. }
  40. /**
  41. * Set a "permanent" cookie. The cookie will last 5 years.
  42. *
  43. * @param string $name
  44. * @param string $value
  45. * @param string $path
  46. * @param string $domain
  47. * @param bool $secure
  48. * @param bool $http_only
  49. * @return bool
  50. */
  51. public function forever($name, $value, $path = '/', $domain = null, $secure = false, $http_only = false)
  52. {
  53. return $this->put($name, $value, 2628000, $path, $domain, $secure, $http_only);
  54. }
  55. /**
  56. * Set the value of a cookie. If a negative number of minutes is
  57. * specified, the cookie will be deleted.
  58. *
  59. * @param string $name
  60. * @param string $value
  61. * @param int $minutes
  62. * @param string $path
  63. * @param string $domain
  64. * @param bool $secure
  65. * @param bool $http_only
  66. * @return bool
  67. */
  68. public function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
  69. {
  70. if ($minutes < 0) unset($_COOKIE[$name]);
  71. return setcookie($name, $value, ($minutes != 0) ? time() + ($minutes * 60) : 0, $path, $domain, $secure, $http_only);
  72. }
  73. /**
  74. * Delete a cookie.
  75. *
  76. * @param string $name
  77. * @return bool
  78. */
  79. public function forget($name)
  80. {
  81. return $this->put($name, null, -60);
  82. }
  83. }