PHP

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
<table>
<?php
  foreach ($_COOKIE as $name => $value) {
    printf('<tr><td>%s</td><td>%s</td></tr>',
      htmlspecialchars($name),
      htmlspecialchars($value));
  }
?>
</table>

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.

Cookies have to be sent prior to any HTML content.

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().

by BrainBellupdated
Advertisement: