PHP Session & PHP Cookies with Example

What is Cookie?

A cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.

Once a cookie has been set, all page requests that follow return the cookie name and value.

A cookie can only be read from the domain that it has been issued from. For example, a cookie set using the domain www.gtupapers.com can not be read from the domain career.gtupapers.com.

Most of the websites on the internet display elements from other domains such as advertising. The domains serving these elements can also set their own cookies. These are known as third party cookies.

A cookie created by a user can only be visible to them. Other users cannot see its value.

Most web browsers have options for disabling cookies, third party cookies or both.

If this is the case then PHP responds by passing the cookie token in the URL.

The diagram shown below illustrates how cookies work.

PHP Cookies and PHP Session

Here,

1) A user requests for a page that stores cookies

2) The server sets the cookie on the user’s computer

3) Other page requests from the user will return the cookie name and value

In this tutorial, you will learn-

Why and when to use Cookies?

Creating Cookies

Let’s now look at the basic syntax used to create a cookie.

<?php

setcookie(cookie_name, cookie_value, [expiry_time], [cookie_path], [domain], [secure], [httponly]);

?>

HERE,

Note: the php set cookie function must be executed before the HTML opening tag.

Let’s now look at an example that uses cookies.

We will create a basic program that allows us to store the user name in a cookie that expires after  ten seconds.

The code below shows the implementation of the above example “cookies.php”.

<?php
     setcookie("user_name", "gtupapers", time()+ 60,'/'); // expires after 60 seconds
     echo 'the cookie has been set for 60 seconds';
?>

Output:

the cookie has been set for 60 seconds

Retrieving the Cookie value

Create another file named “cookies_read.php” with the following code.

<?php
     print_r($_COOKIE);    //output the contents of the cookie array variable 
?>

Output:

Array ( [PHPSESSID] => h5onbf7pctbr0t68adugdp2611 [user_name] => gtupapers )

  Note: $_COOKIE is a PHP built in super global variable.

It contains the names and values of all the set cookies.

The number of values that the

$_COOKIE array can contain depends on the memory size set in php.ini.

The default value is 1GB.

Testing our application.

Let’s assume you have saved your PHP files in phptus folder.

PHP Cookies and PHP Session

Note: Only an empty array has been displayed

PHP Cookies and PHP Session

PHP Cookies and PHP Session

Wait for a minute then click on refresh button again. What results did you get?

Delete Cookies

<?php

 setcookie("user_name", "gtupapers", time() - 360,'/');

?>

What is a Session?

Why and when to use Sessions?

Creating a Session

In order to  create a session, you must first call the PHP session_start function and then store your values in the $_SESSION array variable.

 Let’s suppose we want to know the number of times that a page has been loaded, we can use a session to do that.

The code below shows how to create and retrieve values from sessions

<?php

session_start(); //start the PHP_session function 

if(isset($_SESSION['page_count']))
{
     $_SESSION['page_count'] += 1;
}
else
{
     $_SESSION['page_count'] = 1;
}
 echo 'You are visitor number ' . $_SESSION['page_count'];

?>

Output:

You are visitor number 1

Destroying Session Variables

The session_destroy() function is used to destroy the whole Php session variables.

If you want to destroy only a session single item, you use the unset() function.

The code below illustrates how to use both methods.

<?php

 session_destroy(); //destroy entire session 

?>
<?php

unset($_SESSION['product']); //destroy product session item 

?>

  Session_destroy removes all the session data including cookies associated with the session.

Unset only frees the individual session variables.

Other data remains intact.

Summary

 

YOU MIGHT LIKE: