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.



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





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net