Zend_Http_Cookie-Handling.xml 25 KB

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