Zend_Http_Cookie-Handling.xml 26 KB

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