cookie.php 2.8 KB

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