Приглашаем посетить
Романтизм (19v-euro-lit.niv.ru)

Reading Out Cookies

Previous
Table of Contents
Next

Reading Out Cookies

All cookies to which the server has access (not all cookies that are stored in the browser!) are available in the superglobal array $_COOKIE. The foreach loop reads in all cookies and sends them to the client in an HTML table.

Reading Out Cookie (getcookie.php)
<table>
<?php
  foreach ($_COOKIE as $name => $value) {
    printf('<tr><td>%s</td><td>%s</td></tr>',
      htmlspecialchars($name),
      htmlspecialchars($value));
  }
?>
</table>

WARNING

Because cookies are sent as part of the HTTP header, they have to be created before any Hypertext Markup Language (HTML) content is sent out (unless you are using output buffering). Otherwise, you receive an error message such as the one shown in Figure 5.3.

Figure 5.3. Cookies have to be sent prior to any HTML content.

Reading Out Cookies



NOTE

Usually, PHP takes care of escaping special characters in the cookie values (in URL format). However, since PHP 5, it is possible to do this manually by sending raw cookie data. For this, the function setrawcookie() accepts the same parameter as setcookie(), but does not encode the cookie's value. You must do that manually, with the function urlencode().



Previous
Table of Contents
Next