cookie.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php namespace Laravel; isset($GLOBALS['APP_PATH']) or die('No direct script access.');
  2. use Closure;
  3. if (trim(Config::get('application.key')) === '')
  4. {
  5. throw new \Exception('The cookie class may not be used without an application key.');
  6. }
  7. class Cookie {
  8. /**
  9. * Determine if a cookie exists.
  10. *
  11. * @param string $name
  12. * @return bool
  13. */
  14. public static function has($name)
  15. {
  16. return ! is_null(static::get($name));
  17. }
  18. /**
  19. * Get the value of a cookie.
  20. *
  21. * <code>
  22. * // Get the value of the "favorite" cookie
  23. * $favorite = Cookie::get('favorite');
  24. *
  25. * // Get the value of a cookie or return a default value if it doesn't exist
  26. * $favorite = Cookie::get('framework', 'Laravel');
  27. * </code>
  28. *
  29. * @param string $name
  30. * @param mixed $default
  31. * @return string
  32. */
  33. public static function get($name, $default = null)
  34. {
  35. $value = array_get($_COOKIE, $name);
  36. if ( ! is_null($value) and isset($value[40]) and $value[40] == '~')
  37. {
  38. // The hash signature and the cookie value are separated by a tilde
  39. // character for convenience. To separate the hash and the contents
  40. // we can simply expode on that character.
  41. //
  42. // By re-feeding the cookie value into the "sign" method we should
  43. // be able to generate a hash that matches the one taken from the
  44. // cookie. If they don't, the cookie value has been changed.
  45. list($hash, $value) = explode('~', $value, 2);
  46. if (static::hash($name, $value) === $hash)
  47. {
  48. return $value;
  49. }
  50. }
  51. return value($default);
  52. }
  53. /**
  54. * Set the value of a cookie.
  55. *
  56. * If the response headers have already been sent, the cookie will not be set.
  57. *
  58. * <code>
  59. * // Set the value of the "favorite" cookie
  60. * Cookie::put('favorite', 'Laravel');
  61. *
  62. * // Set the value of the "favorite" cookie for twenty minutes
  63. * Cookie::put('favorite', 'Laravel', 20);
  64. * </code>
  65. *
  66. * @param string $name
  67. * @param string $value
  68. * @param int $minutes
  69. * @param string $path
  70. * @param string $domain
  71. * @param bool $secure
  72. * @return bool
  73. */
  74. public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
  75. {
  76. if (headers_sent()) return false;
  77. $time = ($minutes !== 0) ? time() + ($minutes * 60) : 0;
  78. $_COOKIE[$name] = static::sign($name, $value);
  79. // A cookie payload can't exceed 4096 bytes, so if the payload
  80. // is greater than that, we'll raise an exception to warn the
  81. // developer of the problem since it may cause problems with
  82. // the application, especially if using cookie sessions.
  83. if (strlen($_COOKIE[$name]) > 4000)
  84. {
  85. throw new \Exception("Payload too large for cookie.");
  86. }
  87. return setcookie($name, $_COOKIE[$name], $time, $path, $domain, $secure);
  88. }
  89. /**
  90. * Set a "permanent" cookie. The cookie will last for one year.
  91. *
  92. * <code>
  93. * // Set a cookie that should last one year
  94. * Cookie::forever('favorite', 'Blue');
  95. * </code>
  96. *
  97. * @param string $name
  98. * @param string $value
  99. * @param string $path
  100. * @param string $domain
  101. * @param bool $secure
  102. * @return bool
  103. */
  104. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  105. {
  106. return static::put($name, $value, 525600, $path, $domain, $secure);
  107. }
  108. /**
  109. * Generate a cookie signature based on the contents.
  110. *
  111. * @param string $name
  112. * @param string $value
  113. * @return string
  114. */
  115. protected static function sign($name, $value)
  116. {
  117. return static::hash($name, $value).'~'.$value;
  118. }
  119. /**
  120. * Generate a cookie hash based on the contents.
  121. *
  122. * @param string $name
  123. * @param string $value
  124. * @return string
  125. */
  126. protected static function hash($name, $value)
  127. {
  128. return sha1($name.$value.Config::get('application.key'));
  129. }
  130. /**
  131. * Delete a cookie.
  132. *
  133. * @param string $name
  134. * @param string $path
  135. * @param string $domain
  136. * @param bool $secure
  137. * @return bool
  138. */
  139. public static function forget($name, $path = '/', $domain = null, $secure = false)
  140. {
  141. return static::put($name, null, -2000, $path, $domain, $secure);
  142. }
  143. }