Cooking Cookies with PHP
|Click on a Topic [ Hide ]|
What is a cookie?
|Learn PHP and MySQL fast!|
Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly...
>> Order your copy now for just $39.95 <<
A cookie is often used to store data which can be used to identify a user, for example, person's username.
Cookie is a small flat file which sits on user’s computer. Each time that user requests a page or goes to a webpage, all cookie information is sent too. This is used to identify who you are.
Example of cookie usage:
When you log into a website and check “remember me”, it will store your username (and other information about you) in a cookie. Next time when you come back to the same website, it knows who you are and will log you in automatically.
In this tutorial, we will learn how to write, read and delete cookies in PHP.
Creating a cookie
A cookie can be created using the setcookie function in PHP. Let’s explore this function.
setcookie($name, $value, $expire, $path, $domain, $secure)
- $name - name of the cookie. Example: "username"
- $value - value of the cookie. Example: "john"
- $expire - time (in UNIX timestamp) when the cookie will expire. Example: time()+"3600". Cookie is set to expire after one hour.
- $path - path on the server where cookie will be available.
- $domain - domain where cookie will be available. Instead of path you can use domain settings.
- $secure - true if cookie is being set over a secure "https" server, false otherwise, Default value is false.
For example, if the path is set to "/", the cookie will be available through out the whole site. If the cookie is set to say "/news/", the cookie will only be available under /news/ and all its sub-directories.
If no path is given, cookie in created under the current directory.
For example, if the domian is set to ".yourdomian.com", the cookie will be available within the domain nd all its sub-domains, example news.yourdomain.com.
If the cookie is set say "www.yourdomian.com" the cookie will be available under all www sub-domains, example " www.yourdomian.com/news"
Creating a cookie with setcookie
<?php setcookie("username", "john", time()+3600); ?> <html> <body> </body> </html>
Cookie username is set with value john which is set to expire after one hour on users computer.
The function time() retrieves the current timestamp. Appending 3600 seconds (one hour) to the current time to make the cookie to expire after one hour.
Creating a permanent cookie
Lets create a cookie which is set to last for 1 year.
<?php setcookie("username", "john", time()+(60*60*24*365)); ?>
Retrieving a cookie
Cookie information can retrieved using the predefined $_COOKIE array.
The following will retrieve our username cookie value
<?php echo $_COOKIE["username"]; ?>
To print the entire $_COOKIE array, you can do the following
<?php echo "<pre>"; print_r($_COOKIE); echo "</pre>"; ?>
Array ( [username] => john )
Deleting a Cookie:
In order to delete cookies, you just set the cookie to expire in the past date.
Following will delete our username cookie.
<?php setcookie("username", "john", time()-(60*60*24*365)); ?>
From HTML to PHP programming...
Learn how to build websites in PHP
|<< PHP Date||Top||PHP Sessions >>|