Monday, 18 September 2017

Cookies and Sessions - The PHP way

 Cookies and Sessions - The PHP way

What are cookies?

A cookie is a small file created by a website and is usually stored by the browser in hard disk, they contain information about the user and/or user preferences. Cookies are used to store user states. 
You can locate cookies on your chrome browser by:

1. Open settings


2. Click privacy and security
3. Navigate to content settings

4. Click cookies

5. Finally, scroll down until you find the list of cookies currently stored on your system


How to set up a Cookie in PHP

A cookie is created with the setcookie() function. The usual syntax is 
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly). Note that $name is the only required parameter, the other parameters are optional.
When the setcookie() function is called , a superglobal variable called $_COOKIE is formed. This variable is an associative array consisting of all the cookie attributes.
Let's create a simple cookie
1. create a php file and enter the following code:

<?php

$name = "first_cookie";
$val = "My very first cookie";
$expire = time() + (24*60*60);//The cookie expires in 24 hours
setcookie($name, $val, $expire);


?>
With this, you've created a cookie. We can view the content of the cookie using the echo() function as shown below: 
 <?php

$name = "first_cookie";
$value = "My very first cookie";
$expire = time() + (24*60*60);//The cookie expires in 24 hours
setcookie($name, $value, $expire);
echo $_COOKIE["first_cookie"];
?>

Unsetting a cookie

Unsetting a cookie is just as easy as setting one. To do it one has to set value of the cookie to null and the expiry time to any period in the past.
This can be achieved with a simple line of code:

setcookie($c_name, null, time() -360)

Let's add this to our PHP file, do not forget to  comment out the set cookie function.
<?php

$name = "first_cookie";
$value = "My very first cookie";
$expire = time() + (24*60*60);//The cookie expires in 24 hours
//setcookie($name, $value, $expire);
setcookie($name, null, time() -360);

if (isset($_COOKIE["first_cookie"])){
echo $_COOKIE["first_cookie"];
}else{
echo "cookie unavailable";
}
?>
When you refresh the page 
You have successfully created a deleted a cookie.

Sessions

A session is a server side stored information that exist while a user continues to interact with a website.

Setting up a session

A session on is set using the session_start() php function.
<?php

session_start();
$_SESSION['name'] = 'My_session';
echo $_SESSION['name'] = 'My_session';

?>

The session_start() creates a superglobal called $_SESSION, this contains all the session variables. Note that endless variables can be created.
A session can be deleted by 

session_unset()
This deletes all the session variables

session_destroy()
This destroys the session

Differences Between Cookies and Sessions

Cookies are stored in the client side while sessions are stored on the server side. Sessions are more persistent than cookies. Also, cookies can be deleted from the browser.



No comments:

Post a Comment