cookie.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php namespace Laravel; defined('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 out of the
  44. // cookie. If they don't match, 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. return setcookie($name, $_COOKIE[$name], $time, $path, $domain, $secure);
  80. }
  81. /**
  82. * Set a "permanent" cookie. The cookie will last for one year.
  83. *
  84. * <code>
  85. * // Set a cookie that should last one year
  86. * Cookie::forever('favorite', 'Blue');
  87. * </code>
  88. *
  89. * @param string $name
  90. * @param string $value
  91. * @param string $path
  92. * @param string $domain
  93. * @param bool $secure
  94. * @return bool
  95. */
  96. public static function forever($name, $value, $path = '/', $domain = null, $secure = false)
  97. {
  98. return static::put($name, $value, 525600, $path, $domain, $secure);
  99. }
  100. /**
  101. * Generate a cookie signature based on the contents.
  102. *
  103. * @param string $name
  104. * @param string $value
  105. * @return string
  106. */
  107. protected static function sign($name, $value)
  108. {
  109. return static::hash($name, $value).'~'.$value;
  110. }
  111. /**
  112. * Generate a cookie hash based on the contents.
  113. *
  114. * @param string $name
  115. * @param string $value
  116. * @return string
  117. */
  118. protected static function hash($name, $value)
  119. {
  120. return sha1($name.$value.Config::get('application.key'));
  121. }
  122. /**
  123. * Delete a cookie.
  124. *
  125. * @param string $name
  126. * @param string $path
  127. * @param string $domain
  128. * @param bool $secure
  129. * @return bool
  130. */
  131. public static function forget($name, $path = '/', $domain = null, $secure = false)
  132. {
  133. return static::put($name, null, -2000, $path, $domain, $secure);
  134. }
  135. }