PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie
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
- PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )
- PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )
- PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )
- PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )
- PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )
- PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )
- PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )
- PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables
- PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables
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
Hi @alexendre-maxim
Thank you for your contribution to the video category.
I can see that you are putting in more effort to make your presentation more clear and to the point.
Keep going in this direction as 'focus' is very important in a tutorial to help students grasp concepts easily. The better your focus, the better your presentation.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @rosatravels! Keep up the good work!
Thank you @rosatravels, I will try to do my best to offer the best tutorials.
Why not set these types of cookies in JavaScript, client side? As they appear to be more preference based as opposed to something the server needs to work on and know (php side).
Example and such : https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
@sn0n cookies are used in php as client side and sessions as server side.
Hello @alexendre-maxim! This is a friendly reminder that a Partiko user has just followed you! Congratulations!
To get realtime push notification on your phone about new followers in the future, download and login Partiko using the link below. You will also get 3000 Partiko Points for free, and Partiko Points can be converted into Steem token!
https://partiko.app/referral/partiko
Thank you
Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
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!
Thank you
Hey, @alexendre-maxim!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Thank you