Changes

Jump to: navigation, search

PHP and Cookies - Creating, Reading and Writing

2 bytes added, 20:12, 11 May 2009
no edit summary
<pre>
<?php
setcookie('usernameuserName', 'JohnW', time() + 4800);
echo 'Cookie has been set<br>';
?>
== Reading a Cookie in PHP ==
Gven Given that you've gone to the trouble of writing a cookie it stands to reason you'll probably want to read it back at some point. This is achieved by accessing the $_COOKIE array. The $_COOKIE array is an associative array whereby the name of the cookie provides the index into the array to extract the corresponding value of the name/value pair (for details of PHP arrays read the [[PHP Arrays]] chapter of this book.
For example we can obtain the value of our ''userName'' cookie as follows:
<pre>
echo 'Reading cookie<br>';
echo 'username userName = ' . $_COOKIE['usernameuserName'];
</pre>
<tt>
Reading cookie<br>
username userName = JohnW<br>
</tt>

Navigation menu