Zend_Http_Cookie-Handling.xml 27 KB

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