php Programming
php Applications
MySQL Database
php-MySQL
cakePHP & Joomla
PL-SQL & PHP
Related projects
© The scientific sentence. 2010
| Cookies
It used to identify the user; the server could say: from where
I am getting requests and where I am going to send the related feedback.
Once a request is sent to a server, this server sent an answer along
with a small file (cookie) that will be store in the user's computer.
This cookie will serve as a bridge between the computer's user
and the sever. Cookies can be created and destroyed as well. As
the session, it must be set before the <html> tag.
The syntax is:
setcookie(name, value, expire, path, domain);
Example:
<?php setcookie("atoms", "Noble Gases", time()+60);
//that will expire in one minute.
?>
<html><body> </body></html>
An alternative for setcookie() whose value is an encoded URL when
sent and decoded when received, is setrawcookie().
To rectreive the value of a cookie, we use the variable $_COOKIE.
Example:
displaying the value of the atoms cookie
<?php
echo $_COOKIE["atoms"];
?>
To display all the cookies, we use:
<?php
print_r($_COOKIE);
?>
To delete above cookie, set its time as one minute ago.
<?php //1.
setcookie("atoms", "Noble Gases", time()+60);
//2. setcookie("atoms", "Noble Gases", time()+60);
<?php
// set the expiration date to one hour ago
setcookie("atoms, "", time()-60);
?>
The script:
<?php setcookie("atoms", "Noble Gases", time()+60);
echo $_COOKIE["atoms"];
?>
<html><body>
<?php
echo "<br />";
print_r($_COOKIE);
?>
</body></html>
outputs:
Noble Gases
Array ( [atoms] => Noble Gases [PHPSESSID] => 7eefeda9b41896f0cc3b313c8a3522f6 )
|
|
|