PHP Create,Access,and Destroy Cookies Tutorial

create,access,and destroy cookies in php

Hi Guys,

In this blog,I will explain you how to create,access,and destroy cookies in php a cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.They are typically used to keeping track of information such as a username that the site can retrieve to personalize the page when the utilizer visits the website next time.A cookie can only be read from the domain that it has been issued from.Cookies are conventionally set in an HTTP header but JavaScript can withal set a cookie directly on a browser.

Tip:Each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request.
Setting a Cookie in PHP

The setcookie() function is utilized to set a cookie in PHP. Ascertain you call the setcookie() function before any output engendered by your script otherwise cookie will not set. The basic syntax of this function can be given with:

Syntax:
setcookie(name, value, expire, path, domain, secure);

The parameters of the setcookie() function have the following meanings:

  1. Name:It is used to set the name of the cookie.
  2. Value:It is utilized to set the value of the cookie.
  3. Expire:It is utilized to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
  4. Path:It is utilized to designate the path on the server for which the cookie will be available.
  5. Domain: It is used to specify the domain for which the cookie is available.
  6. Security:It is utilized to denote that the cookie should be sent only if a secure HTTPS connection exists.
Tip:

If the expiration time of the cookie is set to 0, or omitted, the cookie will expire at the end of the session i.e. when the browser closes.

Creating Cookies:

Here's an example that utilizes setcookie() function to engender a cookie named username and assign the value value Dharmik to it. It additionally specify that the cookie will expire after 30 days (30 days * 24 hours * 60 min * 60 sec).

<?php  

  // Setting a cookie
  setcookie("username", "Dharmik", time()+30*24*60*60);

?>
Note:

Only the designation argument in the setcookie() function is obligatory.To skip an argument,the argument can be replaced by a vacuous string(“”).

Warning:

Don't store sensitive data in cookies since it could potentially be manipulated by the maleficent utilizer. To store the sensitive data securely use sessions instead.

Accessing Cookies Values

The PHP $_COOKIE superglobal variable is utilized to retrieve a cookie value. It typically an associative array that contains a list of all the cookies values sent by the browser in the current request, keyed by cookie name. The individual cookie value can be accessed utilizing standard array notation, for example to display the username cookie set in the precedent example, you could utilize the following code.

Example
<?php

  // Accessing an individual cookie value
  echo $_COOKIE["username"];

?>
Output
Dharmik

It's a good practice to check whether a cookie is set or not before accessing its value. To do this you can utilize the PHP isset() function, like this:

Example
<?php

  // Verifying whether a cookie is set or not
  if(isset($_COOKIE["username"])){
      echo "Hi " . $_COOKIE["username"];
  } else{
      echo "Welcome MyWebtuts.com";
  }

?>

You can utilize the print_r() function like print_r($_COOKIE); to optically discern the structure of this $_COOKIE associative array, like you with other arrays.

Removing Cookies

You can delete a cookie by calling the same setcookie() function with the cookie name and any value (such as a empty string) however this time you require the set the expiration date in the past, as shown in the example below:

Example
<?php

// Deleting a cookie
setcookie("username", "", time()-3600);

?>
Note:

The same path, domain, and other arguments should be passed that were used to engender the cookie in order to ascertain that the correct cookie is deleted.

It will help you..

No comments:

Post a Comment