cookie.php 3.6 KB

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