PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie

Image Source

Repository

https://github.com/php/php-src 

What Will I Learn?

I will learn the cookie, the concept of a cookie where we can find it and why we use it , and how to create a cookie or how to save the information about a user , how to modify these cookie and delete it with two ways.

  • What's the cookie .
  • The $_COOKIE array.
  • How to create a cookie using setCookie method.
  • How to modify and delete a cookie.

Requirements

System Requirements:
  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  •  Browser (Chrome for example)  
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  • Basic

Description

1- What's a cookie ?

A cookie is a file that is deposited by your browser on your computer when you surf the Internet. This file is composed only of text, it is totally harmless.

This file is generated by the server of the site you are viewing and it is sent to your Internet browser. This is the browser that will save the file to your hard drive.

A cookie is a mechanism for save and reading data on the client. It's an information that a website asks a browser to memorize.

The cookie is not dangerous in itself, it is only a text file save in a directory according to the browser. No virus can be hidden in a cookie. The data of a cookie can not be read by another site.

2- $_COOKIE Array

The $_COOKIE is an associative array contains all the cookies registered for the user, it has the names of the cookies as keys and the values as values for these keys, it is a superglobal variable like the other arrays $_get, $_post ..etc '.

The cookie array will return all the cookies, it will return an associative array the keys are the names of the cookies and the values are the values for the names.

I have created two cookies the first for the country and the second for the color , I will print it using the $_COOKIE array and this is the result

3- How to create a cookie ? 

to create a cookie we must use the function ' setCookie() ', this method will add the file of cookie in the corresponding path and you can find it easily in your browser to consult it.

This function must be called before any text is displayed on the page. the setCookie() accepts 7 parameters, the first three parameters are useful and the others are optional.

The first parameter is the name of the cookie that we will create.

The second parameter is the content or the value of the cookie.

The third parameter is the expiration time otherwise after how long the cookie will be died or will be overwritten.

We must use the time() method , this method returning the number of seconds elapsed since January 1, 1970 to the present moment and we will add for example 3600 (60 mins).

The fourth is the path that means in which page we will create this cookie.

The fifth parameter is the domain is the site and sub sites depend of our domain , in our case it is the localhost.

The sixth parameter is secure, it will take the true or false value, true for the protection it will use the protocol HTTPS to send this cookie , and false it will be sent by the HTTP protocol.

The seventh parameter is the HTTPonly it is the protocol accepts a boolean value, the true or false value to be just for this protocol and there is not interruption of javascript or it is opened by all.

The example that I want to do is a site of search, I am the user , I will enter my country and the site will give me results from my country

When the user enter the country the cookie will be saved , and when he searches the results will be with his country , this is the result

4- How to modify and Delete Cookie ?  

To modify the cookie you need to create another cookie of same name that it exists with the change of value or other parameter to crush the old one and puts the new one in the array $_COOKIE. this change will also modify the file that is in the user's PC.

And to delete the cookie you have to change the expiration time, if you have to create a cookie to stay for a day for example, you have to add 86400 to the value returned by the time() function, but if you change the operation instead of the addition you put the subtraction, the time will be decreased and the cookie will be deleted, or you can remove it from the browser directly without changing the time.

To modify a cookie you need to change at least one of the parameters of the setCookie method , and the browser will reset the cookie

I have changed the value of the country to " Amercia " , but we have also the " Color" cookie when we print the array of cookies we get this result

And to delete a cookie we need just to modify the time or to delete it automatically from the browser

Or delete it automatically from the browser

Video Tutorial  

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/cookie.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/cookie1.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/cookie2.php

Sort:  

Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published more than 50 posts. Your next target is to reach 60 posts.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.21
TRX 0.26
JST 0.040
BTC 101164.87
ETH 3696.45
USDT 1.00
SBD 3.19