Zend_Http_Cookie-Handling.xml 27 KB

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