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 a defined mechanism as per the business logic.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
On users 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 the 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.
Uses of Cookie
Given below are the uses of cookie:
- To store session_id – A cookie could store the 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 a better user experience based on the preferences set in the cookie file.
Other Important Things about Cookie
Given below are the 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 a cookie, one can mention the instance after which the cookie file will get deleted.
- Storing of Cookie File: The directory or folder where this cookie file is stored is different for different browsers.
- 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 to which machine has a user logged in.
Creating Cookie in PHP
Now let us look at how can one store a cookie.
Code:
<?php setcookie( variable_name_of_cookie, variable_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.
- variable_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 the 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 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.
Code:
<?php setcookie( "variable_name_of_cookie" , "variable_value_of_cookie", current_instance - 10 ); ?>
Now let us understand the code snippet:
- variable_name_of_cookie – This variable shows the cookie’s name, which needs to be deleted. The parameter is a mandatory one. It is this parameter which cookie needs to be operated.
- variable_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. Thus, it helps to specify the user. A cookie in php has wide uses like it can store user preference, etc., 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 & Disadvantages of Cookies
Following are some of the advantages and disadvantages mentioned:
Advantages
- Storing cookies 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 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
- 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 of data; hence it cannot be used to store large data.
- Cookies from advertisements sites could track user personal information like browsing preferences.
Conclusion – Cookie 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. Different websites collect these data. A cookie could be created or deleted as per requirement. It is also used to store other specific data.
The above is the detailed content of Cookie in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
