Zend_Http_Cookie-Handling.xml 28 KB

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