Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Cooking Cookies with PHP (0 Comments)
Admin: Posted Date: March 3, 2010

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.

What is a cookie?

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.
  • 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.

  • $domain - domain where cookie will be available. Instead of path you can use domain settings.
  • 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"

  • $secure - true if cookie is being set over a secure "https" server, false otherwise, Default value is false.

Creating a cookie with setcookie


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.

Note: A cookie must be set before any HTML code as shown above.

Creating a permanent cookie

Lets create a cookie which is set to last for 1 year.


Retrieving a cookie

Cookie information can retrieved using the predefined $_COOKIE array.

The following will retrieve our username cookie value



Output

john

To print the entire $_COOKIE array, you can do the following

";
print_r($_COOKIE);
echo "
"; ?>

Output

Array
(
[username] => john
)

Deleting a Cooki

google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);In order to delete cookies, you just set the cookie to expire in the past date.

Following will delete our username cookie.


PHP Sessions

Welcome! In this section we cover all important sessions in PHP.

Sessions are used to store information which can be used through-out the application for the entire time the user is logged in or running the application. Each time a user visits your website, you can create a session for that user to store information pertaining to that user.

Unlike other applications, in web applications, all the stored variables and objects are not globally available throughout all pages (unless sent through POST or GET methods to the next page), so to tackle this problem sessions are utilized.

A usage of sessions would be for example, storing products in a shopping cart. On your favorite merchant site, when you, the user, clicks on "add to shopping cart", the product information is then stored in a session (using session variables) which is then available on the server for later use for the entire the time you browse the website.

Sessions in PHP

In PHP, information is stored in session variables. Now lets learn how we create a session in PHP

Before you can store any information in session variables, you must first start up the session using the session_start() function. See below.


When you start a session a unique session id (PHPSESSID) for each visitor/user is created. You can access the session id using the PHP predefined constant PHPSESSID.

The code above starts a session for the user on the server, and assign a session id for that user's session.

Note: The session_start() function must appear BEFORE the tag

Storing information in a session

To store information is a session variable, you must use the predefined session variable $_SESSION.

Now, as an example, lets store user's name and their favorite color in a session. To do so you would do the following.


Retrieving stored session information

google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);Retrieving stored session information is really easy. You can access the stored session information on any page without doing anything extra.

";
echo $_SESSION["color"];
?>

Output

Johny
blue

Destroying/deleting session information

Remember sessions are destroyed automatically after user leaves your website or closes the browser, but if you wish to clear a session variable yourself, you can simple use the unset() function to clean the session variable.


To completely destroy all session variables in one go, you can use the session_destroy() function.


 

 

 

 

 

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: