Zend_Http_Cookie-Handling.xml 22 KB

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