cookie.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php namespace Laravel; use Closure;
  2. class Cookie {
  3. /**
  4. * The cookies that have been set.
  5. *
  6. * @var array
  7. */
  8. public static $jar = array();
  9. /**
  10. * Determine if a cookie exists.
  11. *
  12. * @param string $name
  13. * @return bool
  14. */
  15. public static function has($name)
  16. {
  17. return ! is_null(static::get($name));
  18. }
  19. /**
  20. * Get the value of a cookie.
  21. *
  22. * <code>
  23. * // Get the value of the "favorite" cookie
  24. * $favorite = Cookie::get('favorite');
  25. *
  26. * // Get the value of a cookie or return a default value
  27. * $favorite = Cookie::get('framework', 'Laravel');
  28. * </code>
  29. *
  30. * @param string $name
  31. * @param mixed $default
  32. * @return string
  33. */
  34. public static function get($name, $default = null)
  35. {
  36. if (isset(static::$jar[$name])) return static::$jar[$name];
  37. return array_get(Request::foundation()->cookies->all(), $name, $default);
  38. }
  39. /**
  40. * Set the value of a cookie.
  41. *
  42. * <code>
  43. * // Set the value of the "favorite" cookie
  44. * Cookie::put('favorite', 'Laravel');
  45. *
  46. * // Set the value of the "favorite" cookie for twenty minutes
  47. * Cookie::put('favorite', 'Laravel', 20);
  48. * </code>
  49. *
  50. * @param string $name
  51. * @param string $value
  52. * @param int $expiration
  53. * @param string $path
  54. * @param string $domain
  55. * @param bool $secure
  56. * @return void
  57. */
  58. public static function put($name, $value, $expiration = 0, $path = '/', $domain = null, $secure = false)
  59. {
  60. if ($expiration !== 0)
  61. {
  62. $expiration = time() + ($expiration * 60);
  63. }
  64. // If the secure option is set to true, yet the request is not over HTTPS
  65. // we'll throw an exception to let the developer know that they are
  66. // attempting to send a secure cookie over the unsecure HTTP.
  67. if ($secure and ! Request::secure())
  68. {
  69. throw new \Exception("Attempting to set secure cookie over HTTP.");
  70. }
  71. static::$jar[$name] = compact('name', 'value', 'expiration', 'path', 'domain', 'secure');
  72. }
  73. /**
  74. * Set a "permanent" cookie. The cookie will last for one year.
  75. *
  76. * <code>
  77. * // Set a cookie that should last one year
  78. * Cookie::forever('favorite', 'Blue');
  79. * </code>
  80. *
  81. * @param string $name
  82. * @param string $value
  83. * @param string $path
  84. * @param string $domain
  85. * @param bool $secure
  86. * @return bool
  87. */
  88. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  89. {
  90. return static::put($name, $value, 525600, $path, $domain, $secure);
  91. }
  92. /**
  93. * Delete a cookie.
  94. *
  95. * @param string $name
  96. * @param string $path
  97. * @param string $domain
  98. * @param bool $secure
  99. * @return bool
  100. */
  101. public static function forget($name, $path = '/', $domain = null, $secure = false)
  102. {
  103. return static::put($name, null, -2000, $path, $domain, $secure);
  104. }
  105. }