Introduction to Cookie in PHP
The following article Cookie in PHP provides a detailed outline of the cookie in PHP. PHP is one of the back-end technology which is generally used for making web applications. A web-application generally has authentication A server authenticates the user by defined mechanism as per the business logic.
On user getting authenticated, we generally use session_id to authenticate subsequent user requests. Now, this session_id is created on the server-side. However, in every subsequent request from the client, this session_id must be received from the user side. Hence, there needs to be a file in which we can store session_id. To store such session_id on user desktop we have a concept of cookie. This cookie file could be used to store the session_id of the user. For subsequent requests from the client-side, the session_id is read from the cookie file and is then used in the request. A cookie in PHP is generally used to identify a user. As explained above, this cookie stores information like session_id which serves for the purpose of user authentication. A cookie file stores more information like user name, its value, etc. In this article, we will discuss Cookie in PHP in detail.
Uses of Cookie
- To store session_id – A cookie could store session_id of the user. This stored session_id is secured and hence could be used to read session_id on request to the server.
- To provide better user preference – A cookie could be used to provide better user experience based on the preferences set in the cookie file.
Other Important Things about Cookie
- File Size of 4KB
The file size of a cookie can be a maximum of 4KB.
- A Cookie Created by a Website can be Read-Only by the Website Created it
A cookie created by a website could be only read and used by that particular website only. Another website could not read cookies created by the other website.
- Can Store Instance when Cookie_id will be Destroyed
While creating cookie one can mention the instance after which cookie file will get deleted.
- Storing of Cookie File
Directory or folder where this cookie file is stored is different for different browsers.
4.8 (3,302 ratings)
- Unique to the machine
A cookie is valid for a particular machine only. A cookie is not specific to who has logged into the website but more specific from which machine has a user logged in.
Creating Cookie in PHP
Now let us look how can one store a cookie
setcookie( variable_name_of_cookie, varaible_value_of_cookie, [ instance_after_which_cookie_gets_deleted], [path_of_the_cookie_created], [domain], [secure], [httponly] )
Now, let us try to understand the above-mentioned code:
- variable_name_of_cookie – This variable stores the name of the cookie. The parameter is a mandatory one. It is this parameter that is used to retrieve the value stored in a cookie.
- varaible_value_of_cookie – This is another mandatory parameter that stores the value of the cookie. It stores the value of the cookie variable which is created.
- instance_after_which_cookie_gets_deleted – It shows the instance after which cookie will be deleted.
- path_of_the_cookie_created – This parameter is optional. It is used to specify the path where a cookie is created on the server.
- domain – domain is another optional parameter. This parameter specifies the hierarchy across which the cookie will be present.
- secure – It is an optional parameter, and specifies whether a cookie needs to be communicated between server and machine using a secured https protocol or not. By default, its value is set false and uses HTTP protocol else if specified otherwise then it uses https protocol.
- httponly – This is parameter specifies whether client-side language could use this cookie of the server.
Now with that let us see how can a cookie be deleted.
Deleting a Cookie
It is quite easy to delete a cookie. Following code, the snippet could be used to delete a cookie.
setcookie( “variable_name_of_cookie” , “varaible_value_of_cookie”, current_instance - 10 );
Now let us understand the code snippet
- variable_name_of_cookie – This variable shows the name of the cookie which needs to be deleted. The parameter is a mandatory one. It is this parameter which cookie needs to be operated
- varaible_value_of_cookie – This is another mandatory parameter that specifies the value to be assigned to the cookie variable. It is generally assigned as blank
- instance_at_which_cookie_gets_deleted – Shows at which instance cookie needs to be deleted
Working of Cookies in PHP
A cookie is used to specify the identity of a user. It helps to specify the user. A cookie in php has wide uses like it can be used to store user preference etc so as to modify user experiences.
Management of Cookies
Here we will see how can we disable cookies in Google Chrome.
- Click on control+shift+delete.
- It will show a new dialog box.
- Click on the cookie checkbox.
- Click on the dialog button.
Advantages of Cookie
- Storing cookie is lighter as it does not puts extra load on the server. It is generally stored on a client machine
- A cookie can be configured easily.
- Using cookies it can be used to store session information like pages or threads etc.
- Cookies once stored could be used later also without creating cookies.
- Cookies are used to personalize the user preferences.
- Based on user preferences, cookies could be used to show similar types of advertisements to a user.
- Cookies can be used to make browsing easier.
Disadvantages of Cookie
- A cookie is not recommended to store data that needs to be secured. Content in cookies is plain text once only those data could be stored which is not security concerned.
- Encrypting and decrypting cookies data is not meaningful as it required extra coding leading to resource extra responsibilities.
- A cookie can store a maximum of 4 KB data hence it cannot be used to store large data.
- Cookies from advertisements sites could track user personal information like browsing preferences.
Conclusion – Cookies in PHP
A cookie is widely used in web-based applications. It is used to recognize the user A cookie is used to store user preferences like which website a user is surfing etc. These data are collected by different websites. A cookie could be created or deleted as per requirement. It is also used to store other specific data.
This is a guide to Cookie in PHP. Here we discuss Cookie in PHP, Uses of Cookies, Management, Advantages and Disadvantages of Cookie. You can also go through our other suggested articles –