2
0

Zend_Http_Cookie-Handling.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.http.cookies">
  4. <title>Zend_Http_Cookie and Zend_Http_CookieJar</title>
  5. <sect2 id="zend.http.cookies.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. Zend_Http_Cookie, as expected, is a class that represents an HTTP
  9. cookie. It provides methods for parsing HTTP response strings,
  10. collecting cookies, and easily accessing their properties. It also
  11. allows checking if a cookie matches against a specific scenario, IE
  12. a request URL, expiration time, secure connection, etc.
  13. </para>
  14. <para>
  15. Zend_Http_CookieJar is an object usually used by Zend_Http_Client to
  16. hold a set of Zend_Http_Cookie objects. The idea is that if a
  17. Zend_Http_CookieJar object is attached to a Zend_Http_Client object,
  18. all cookies going from and into the client through HTTP requests and
  19. responses will be stored by the CookieJar object. Then, when the client
  20. will send another request, it will first ask the CookieJar object for
  21. all cookies matching the request. These will be added to the request
  22. headers automatically. This is highly useful in cases where you need to
  23. maintain a user session over consecutive HTTP requests, automatically
  24. sending the session ID cookies when required. Additionally, the
  25. Zend_Http_CookieJar object can be serialized and stored in $_SESSION
  26. when needed.
  27. </para>
  28. </sect2>
  29. <sect2 id="zend.http.cookies.cookie.instantiating">
  30. <title>Instantiating Zend_Http_Cookie Objects</title>
  31. <para>
  32. Instantiating a Cookie object can be done in two ways:
  33. <itemizedlist>
  34. <listitem>
  35. <para>
  36. Through the constructor, using the following syntax:
  37. <code>new Zend_Http_Cookie(string $name, string $value, string $domain, [int $expires, [string $path, [boolean $secure]]]);</code>
  38. </para>
  39. <itemizedlist>
  40. <listitem>
  41. <para>
  42. <code>$name</code>: The name of the cookie (eg. 'PHPSESSID') (required)
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <code>$value</code>: The value of the cookie (required)
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <code>$domain</code>: The cookie's domain (eg. '.example.com') (required)
  53. </para>
  54. </listitem>
  55. <listitem>
  56. <para>
  57. <code>$expires</code>: Cookie expiration time, as UNIX time stamp (optional, defaults to null).
  58. If not set, cookie will be treated as a 'session cookie' with no expiration time.
  59. </para>
  60. </listitem>
  61. <listitem>
  62. <para>
  63. <code>$path</code>: Cookie path, eg. '/foo/bar/' (optional, defaults to '/')
  64. </para>
  65. </listitem>
  66. <listitem>
  67. <para>
  68. <code>$secure</code>: Boolean, Whether the cookie is to be sent over secure (HTTPS)
  69. connections only (optional, defaults to boolean FALSE)
  70. </para>
  71. </listitem>
  72. </itemizedlist>
  73. </listitem>
  74. <listitem>
  75. <para>
  76. By calling the fromString() static method, with a cookie string as represented in the
  77. 'Set-Cookie' HTTP response header or 'Cookie' HTTP request header. In this case, the
  78. cookie value must already be encoded. When the cookie string does not contain a 'domain'
  79. part, you must provide a reference URI according to which the cookie's domain and path
  80. will be set.
  81. </para>
  82. </listitem>
  83. </itemizedlist>
  84. <example id="zend.http.cookies.cookie.instantiating.example-1">
  85. <title>Instantiating a Zend_Http_Cookie object</title>
  86. <programlisting language="php"><![CDATA[
  87. // First, using the constructor. This cookie will expire in 2 hours
  88. $cookie = new Zend_Http_Cookie('foo',
  89. 'bar',
  90. '.example.com',
  91. time() + 7200,
  92. '/path');
  93. // You can also take the HTTP response Set-Cookie header and use it.
  94. // This cookie is similar to the previous one, only it will not expire, and
  95. // will only be sent over secure connections
  96. $cookie = Zend_Http_Cookie::fromString('foo=bar; domain=.example.com; ' .
  97. 'path=/path; secure');
  98. // If the cookie's domain is not set, you have to manually specify it
  99. $cookie = Zend_Http_Cookie::fromString('foo=bar; secure;',
  100. 'http://www.example.com/path');
  101. ]]></programlisting>
  102. </example>
  103. <note>
  104. <para>
  105. When instantiating a cookie object using the Zend_Http_Cookie::fromString() method, the
  106. cookie value is expected to be URL encoded, as cookie strings should be. However, when
  107. using the constructor, the cookie value string is expected to be the real, decoded value.
  108. </para>
  109. </note>
  110. </para>
  111. <para>
  112. A cookie object can be transferred back into a string, using the __toString() magic method.
  113. This method will produce a HTTP request "Cookie" header string, showing the cookie's name
  114. and value, and terminated by a semicolon (';').
  115. The value will be URL encoded, as expected in a Cookie header:
  116. <example id="zend.http.cookies.cookie.instantiating.example-2">
  117. <title>Stringifying a Zend_Http_Cookie object</title>
  118. <programlisting language="php"><![CDATA[
  119. // Create a new cookie
  120. $cookie = new Zend_Http_Cookie('foo',
  121. 'two words',
  122. '.example.com',
  123. time() + 7200,
  124. '/path');
  125. // Will print out 'foo=two+words;' :
  126. echo $cookie->__toString();
  127. // This is actually the same:
  128. echo (string) $cookie;
  129. // In PHP 5.2 and higher, this also works:
  130. echo $cookie;
  131. ]]></programlisting>
  132. </example>
  133. </para>
  134. </sect2>
  135. <sect2 id="zend.http.cookies.cookie.accessors">
  136. <title>Zend_Http_Cookie getter methods</title>
  137. <para>
  138. Once a Zend_Http_Cookie object is instantiated, it provides several getter methods to get
  139. the different properties of the HTTP cookie:
  140. <itemizedlist>
  141. <listitem>
  142. <para>
  143. <code>string getName()</code>: Get the name of the cookie
  144. </para>
  145. </listitem>
  146. <listitem>
  147. <para>
  148. <code>string getValue()</code>: Get the real, decoded value of the cookie
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. <code>string getDomain()</code>: Get the cookie's domain
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <code>string getPath()</code>: Get the cookie's path, which defaults to '/'
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <code>int getExpiryTime()</code>: Get the cookie's expiration time, as UNIX time stamp. If
  164. the cookie has no expiration time set, will return NULL.
  165. </para>
  166. </listitem>
  167. </itemizedlist>
  168. </para>
  169. <para>
  170. Additionally, several boolean tester methods are provided:
  171. <itemizedlist>
  172. <listitem>
  173. <para>
  174. <code>boolean isSecure()</code>: Check whether the cookie is set to be sent over secure
  175. connections only. Generally speaking, if true the cookie should only be sent over HTTPS.
  176. </para>
  177. </listitem>
  178. <listitem>
  179. <para>
  180. <code>boolean isExpired(int $time = null)</code>: Check whether the cookie is expired or not.
  181. If the cookie has no expiration time, will always return true. If $time is provided, it will
  182. override the current time stamp as the time to check the cookie against.
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. <code>boolean isSessionCookie()</code>: Check whether the cookie is a "session cookie" - that
  188. is a cookie with no expiration time, which is meant to expire when the session ends.
  189. </para>
  190. </listitem>
  191. </itemizedlist>
  192. </para>
  193. <para>
  194. <example id="zend.http.cookies.cookie.accessors.example-1">
  195. <title>Using getter methods with Zend_Http_Cookie</title>
  196. <programlisting language="php"><![CDATA[
  197. // First, create the cookie
  198. $cookie =
  199. Zend_Http_Cookie::fromString('foo=two+words; ' +
  200. 'domain=.example.com; ' +
  201. 'path=/somedir; ' +
  202. 'secure; ' +
  203. 'expires=Wednesday, 28-Feb-05 20:41:22 UTC');
  204. echo $cookie->getName(); // Will echo 'foo'
  205. echo $cookie->getValue(); // will echo 'two words'
  206. echo $cookie->getDomain(); // Will echo '.example.com'
  207. echo $cookie->getPath(); // Will echo '/'
  208. echo date('Y-m-d', $cookie->getExpiryTime());
  209. // Will echo '2005-02-28'
  210. echo ($cookie->isExpired() ? 'Yes' : 'No');
  211. // Will echo 'Yes'
  212. echo ($cookie->isExpired(strtotime('2005-01-01') ? 'Yes' : 'No');
  213. // Will echo 'No'
  214. echo ($cookie->isSessionCookie() ? 'Yes' : 'No');
  215. // Will echo 'No'
  216. ]]></programlisting>
  217. </example>
  218. </para>
  219. </sect2>
  220. <sect2 id="zend.http.cookies.cookie.matching">
  221. <title>Zend_Http_Cookie: Matching against a scenario</title>
  222. <para>
  223. The only real logic contained in a Zend_Http_Cookie object, is in the match() method.
  224. This method is used to test a cookie against a given HTTP request scenario, in order
  225. to tell whether the cookie should be sent in this request or not. The method has
  226. the following syntax and parameters:
  227. <code>boolean Zend_Http_Cookie->match(mixed $uri, [boolean $matchSessionCookies, [int $now]]);</code>
  228. <itemizedlist>
  229. <listitem>
  230. <para>
  231. <code>mixed $uri</code>: A Zend_Uri_Http object with a domain name and path to be checked.
  232. Optionally, a string representing a valid HTTP URL can be passed instead. The cookie will
  233. match if the URL's scheme (HTTP or HTTPS), domain and path all match.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <code>boolean $matchSessionCookies</code>: Whether session cookies should be matched or not.
  239. Defaults to true. If set to false, cookies with no expiration time will never match.
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <code>int $now</code>: Time (represented as UNIX time stamp) to check a cookie against
  245. for expiration. If not specified, will default to the current time.
  246. </para>
  247. </listitem>
  248. </itemizedlist>
  249. <example id="zend.http.cookies.cookie.matching.example-1">
  250. <title>Matching cookies</title>
  251. <programlisting language="php"><![CDATA[
  252. // Create the cookie object - first, a secure session cookie
  253. $cookie = Zend_Http_Cookie::fromString('foo=two+words; ' +
  254. 'domain=.example.com; ' +
  255. 'path=/somedir; ' +
  256. 'secure;');
  257. $cookie->match('https://www.example.com/somedir/foo.php');
  258. // Will return true
  259. $cookie->match('http://www.example.com/somedir/foo.php');
  260. // Will return false, because the connection is not secure
  261. $cookie->match('https://otherexample.com/somedir/foo.php');
  262. // Will return false, because the domain is wrong
  263. $cookie->match('https://example.com/foo.php');
  264. // Will return false, because the path is wrong
  265. $cookie->match('https://www.example.com/somedir/foo.php', false);
  266. // Will return false, because session cookies are not matched
  267. $cookie->match('https://sub.domain.example.com/somedir/otherdir/foo.php');
  268. // Will return true
  269. // Create another cookie object - now, not secure, with expiration time
  270. // in two hours
  271. $cookie = Zend_Http_Cookie::fromString('foo=two+words; ' +
  272. 'domain=www.example.com; ' +
  273. 'expires='
  274. . date(DATE_COOKIE, time() + 7200));
  275. $cookie->match('http://www.example.com/');
  276. // Will return true
  277. $cookie->match('https://www.example.com/');
  278. // Will return true - non secure cookies can go over secure connections
  279. // as well!
  280. $cookie->match('http://subdomain.example.com/');
  281. // Will return false, because the domain is wrong
  282. $cookie->match('http://www.example.com/', true, time() + (3 * 3600));
  283. // Will return false, because we added a time offset of +3 hours to
  284. // current time
  285. ]]></programlisting>
  286. </example>
  287. </para>
  288. </sect2>
  289. <sect2 id="zend.http.cookies.cookiejar">
  290. <title>The Zend_Http_CookieJar Class: Instantiation</title>
  291. <para>
  292. In most cases, there is no need to directly instantiate a
  293. Zend_Http_CookieJar object. If you want to attach a new cookie jar
  294. to your Zend_Http_Client object, just call the
  295. Zend_Http_Client->setCookieJar() method, and a new, empty cookie jar
  296. will be attached to your client. You could later get this cookie jar
  297. using Zend_Http_Client->getCookieJar().
  298. </para>
  299. <para>
  300. If you still wish to manually instantiate a CookieJar object, you
  301. can do so by calling "new Zend_Http_CookieJar()" directly - the
  302. constructor method does not take any parameters. Another way to
  303. instantiate a CookieJar object is to use the static Zend_Http_CookieJar::fromResponse()
  304. method. This method takes two parameters: a Zend_Http_Response object,
  305. and a reference URI, as either a string or a Zend_Uri_Http object.
  306. This method will return a new Zend_Http_CookieJar object, already
  307. containing the cookies set by the passed HTTP response. The reference
  308. URI will be used to set the cookie's domain and path, if they are
  309. not defined in the Set-Cookie headers.
  310. </para>
  311. </sect2>
  312. <sect2 id="zend.http.cookies.cookiejar.adding_cookies">
  313. <title>Adding Cookies to a Zend_Http_CookieJar object</title>
  314. <para>
  315. Usually, the Zend_Http_Client object you attached your CookieJar object
  316. to will automatically add cookies set by HTTP responses to your jar. If
  317. you wish to manually add cookies to your jar, this can be done by using
  318. two methods:
  319. <itemizedlist>
  320. <listitem>
  321. <para>
  322. <classname>Zend_Http_CookieJar->addCookie($cookie[, $ref_uri])</classname>: Add a
  323. single cookie to the jar. $cookie can be either a Zend_Http_Cookie
  324. object or a string, which will be converted automatically into a
  325. Cookie object. If a string is provided, you should also provide
  326. $ref_uri - which is a reference URI either as a string or
  327. Zend_Uri_Http object, to use as the cookie's default domain and path.
  328. </para>
  329. </listitem>
  330. <listitem>
  331. <para>
  332. <classname>Zend_Http_CookieJar->addCookiesFromResponse($response, $ref_uri)</classname>:
  333. Add all cookies set in a single HTTP response to the jar. $response is
  334. expected to be a Zend_Http_Response object with Set-Cookie headers. $ref_uri
  335. is the request URI, either as a string or a Zend_Uri_Http object, according
  336. to which the cookies' default domain and path will be set.
  337. </para>
  338. </listitem>
  339. </itemizedlist>
  340. </para>
  341. </sect2>
  342. <sect2 id="zend.http.cookies.cookiejar.getting_cookies">
  343. <title>Retrieving Cookies From a Zend_Http_CookieJar object</title>
  344. <para>
  345. Just like with adding cookies, there is usually no need to manually
  346. fetch cookies from a CookieJar object. Your Zend_Http_Client object
  347. will automatically fetch the cookies required for an HTTP request
  348. for you. However, you can still use 3 provided methods to fetch
  349. cookies from the jar object: <code>getCookie()</code>,
  350. <code>getAllCookies()</code>, and <code>getMatchingCookies()</code>.
  351. Additionnaly, iterating over the CookieJar will let you
  352. retrieve all the Zend_Http_Cookie objects from it.
  353. </para>
  354. <para>
  355. It is important to note that each one of these methods takes a
  356. special parameter, which sets the return type of the method. This
  357. parameter can have 3 values:
  358. <itemizedlist>
  359. <listitem>
  360. <para>
  361. <classname>Zend_Http_CookieJar::COOKIE_OBJECT</classname>: Return
  362. a Zend_Http_Cookie object. If the method returns more than
  363. one cookie, an array of objects will be returned.
  364. </para>
  365. </listitem>
  366. <listitem>
  367. <para>
  368. <classname>Zend_Http_CookieJar::COOKIE_STRING_ARRAY</classname>: Return
  369. cookies as strings, in a "foo=bar" format, suitable for sending
  370. in a HTTP request "Cookie" header. If more than one cookie is
  371. returned, an array of strings is returned.
  372. </para>
  373. </listitem>
  374. <listitem>
  375. <para>
  376. <classname>Zend_Http_CookieJar::COOKIE_STRING_CONCAT</classname>: Similar to
  377. COOKIE_STRING_ARRAY, but if more than one cookie is returned, this
  378. method will concatenate all cookies into a single, long string
  379. separated by semicolons (;), and return it. This is especially useful
  380. if you want to directly send all matching cookies in a single HTTP
  381. request "Cookie" header.
  382. </para>
  383. </listitem>
  384. </itemizedlist>
  385. </para>
  386. <para>
  387. The structure of the different cookie-fetching methods is described below:
  388. <itemizedlist>
  389. <listitem>
  390. <para>
  391. <classname>Zend_Http_CookieJar->getCookie($uri, $cookie_name[, $ret_as])</classname>:
  392. Get a single cookie from the jar, according to its URI (domain and path)
  393. and name. $uri is either a string or a Zend_Uri_Http object representing the
  394. URI. $cookie_name is a string identifying the cookie name. $ret_as
  395. specifies the return type as described above. $ret_type is optional, and
  396. defaults to COOKIE_OBJECT.
  397. </para>
  398. </listitem>
  399. <listitem>
  400. <para>
  401. <classname>Zend_Http_CookieJar->getAllCookies($ret_as)</classname>: Get all
  402. cookies from the jar. $ret_as specifies the return type as described
  403. above. If not specified, $ret_type defaults to COOKIE_OBJECT.
  404. </para>
  405. </listitem>
  406. <listitem>
  407. <para>
  408. <classname>Zend_Http_CookieJar->getMatchingCookies($uri[, $matchSessionCookies[, $ret_as[, $now]]])</classname>:
  409. Get all cookies from the jar that match a specified scenario, that is a URI and expiration time.
  410. <itemizedlist>
  411. <listitem>
  412. <para>
  413. <code>$uri</code> is either a Zend_Uri_Http object or a string specifying the
  414. connection type (secure or non-secure), domain and path to match against.
  415. </para>
  416. </listitem>
  417. <listitem>
  418. <para>
  419. <code>$matchSessionCookies</code> is a boolean telling whether to match session
  420. cookies or not. Session cookies are cookies that have no specified expiration
  421. time. Defaults to true.
  422. </para>
  423. </listitem>
  424. <listitem>
  425. <para>
  426. <code>$ret_as</code> specifies the return type as described above. If not
  427. specified, defaults to COOKIE_OBJECT.
  428. </para>
  429. </listitem>
  430. <listitem>
  431. <para>
  432. <code>$now</code> is an integer representing the UNIX time stamp to consider
  433. as "now" - that is any cookies who are set to expire before this time will
  434. not be matched. If not specified, defaults to the current time.
  435. </para>
  436. </listitem>
  437. </itemizedlist>
  438. You can read more about cookie matching here:
  439. <xref linkend="zend.http.cookies.cookie.matching" />.
  440. </para>
  441. </listitem>
  442. </itemizedlist>
  443. </para>
  444. </sect2>
  445. </sect1>
  446. <!--
  447. vim:se ts=4 sw=4 et:
  448. -->