This article briefly explains the origin and fundamental properties of cookies, provides technical background for learning how to set cookies in PHP, and recommends PHP novices to read it.
Before learning how to set Cookies in PHP, let's first understand Cookie technology.
(1) Introduction to Cookie Technology
In the history of WEB technology development, the emergence of Cookie technology is a major change. First, Netscape introduced Cookie technology in its Netscape Navigator browser. Since then, the World Wide Web Association has begun to support the Cookie standard. Later, after vigorous promotion by Microsoft (because the ASP technology used by Microsoft's IIS web server heavily used Cookie technology), Cookie technology was fully supported in Microsoft's Internet Explorer browser. Up to now, the vast majority of browsers support Cookie technology, or are at least compatible with Cookies
Use of technology.
1) What are cookies?
According to the definition in Netscape's official documentation, cookies are a way for a server or script to maintain information on the client's workstation under the HTTP protocol. Cookies are small text files saved on the user's browser by the Web server, which can contain information about the user (such as identification number, password, how the user purchases on the Web site or the number of times the user visits the site). Whenever a user connects to the server, the Web site can access the cookie information.
In layman's terms, browsers use one or more limited files to support cookies. These files are called cookie files on machines using Windows operating systems and magic cookie files on Macintosh machines. These files are used by websites to store cookie data on them.
Websites can insert information into these cookie files, which may have side effects for some Internet users. Some users believe that this causes an invasion of personal privacy. What's worse, some people believe that cookies are an invasion of personal space and can cause security hazards to users' computers.
Currently some cookies are temporary and others are persistent. Temporary cookies are only saved on the browser for a specified period of time. Once the specified period of time expires, the cookie will be cleared by the system. For example, in PHP, cookies are used to track user progress until the user leaves the website. A persistent cookie is saved in the user's cookie file and can still be called the next time the user returns.
Saving cookies in a cookie file, some users will go too far and think this will cause big problems. The main reason is that some users are worried that cookies will track users' online surfing habits, such as which types of sites users like to visit and what activities they like to engage in. I am afraid that once this kind of personal information falls into the hands of some people with ulterior motives, the individual may become the target of a lot of advertising garbage, or even suffer unexpected damage. However, this worry will not occur at all, because users outside the website cannot obtain cookie information across the website. So it is impossible to use cookies for this purpose. However, due to some users' misunderstandings and "false rumors", some browser developers have no choice but to make familiar responses (for example, Netscape Navigator 4.0 and Internet Explorer 3.0 both provide the option to block cookies).
The result of waiting for Cookie technology for so long is that it forces many browser developers to provide flexible control over Cookies in their browsers. For example, the current two major browsers, Netscape Navigator and Internet Explorer, handle cookies in this way
: Netscape Navigator 4.0 can not only accept Cookie warnings, but also block Cookies; Internet Explorer 3.0 can also block Cookies, but in Internet Explorer 4.0 it can only accept warnings without providing a blocking option. However, in Internet
Updated versions after Explorer 4.0 have added the option to block cookies.
In addition, many of the latest technologies can even block cookies on browsers that cannot block cookies. For example, you can limit the use of cookies by setting cookie files to different types. However, unfortunately, if you want to completely block cookies, you will definitely reject many site pages. Because today many Web site developers have fallen in love with the powerful functions of Cookie technology. For example, the use of Session objects cannot be separated from the support of Cookies.
Although there are still some Internet users today who are still arguing about Cookies, the vast majority of Internet users still tend to accept Cookies. Therefore, we can safely use Cookie technology to develop our WEB pages.
2) How do cookies work?
To understand cookies, it is essential to know how they work. Generally speaking, cookies are returned from the server to the browser through HTTP Headers. First, the server uses the Set-Cookie header in the response to create a Cookie. Then, the browser includes the created Cookie in its request through the Cookie header, and returns it to the server, thereby completing the browser's argument.
For example, we created a cookie named login to contain the visitor's information. When creating the cookie, the server-side header is as shown below. It is assumed that the visitor's registered name is "Michael Jordan", and the created cookie is also Attributes such as path,
domain, expires, etc. are specified.
Set-Cookie:login=Michael Jordan;path=/;domain=msn.com;
expires=Monday,01-Mar-99 00:00:01 GMT
The header above will automatically add a record to the cookie file of the browser computer. The browser assigns the value of the cookie with the variable name "login" to "Michael Jordon". Note that during the actual delivery process, the value of this cookie passes through the URLEncode method.
URL encoding operation. After the HTTP Header containing the Cookie value is saved to the browser's Cookie file, the Header notifies the browser to return the Cookie to the server by ignoring the path through the request, completing the browser's authentication operation.
In addition, we use some attributes of the cookie to limit the use of the cookie. For example, the Domain attribute can limit the sending of cookies on the browser side. In the above example, the cookie can only be sent to the designated server, and will never go to other Web sites such as www.phpq.net . . The Expires attribute specifies the time period for which the cookie is saved. For example, the cookie above is only saved on the browser for 1 second on March 1, 1999. Of course, if there are too many cookies on the browser and exceed the range allowed by the system, the browser will automatically delete them. As for the attribute Path, it is used to specify which directory path the cookie will be sent to the server.
Note: After the browser creates a cookie, every request for this website will carry this cookie in the header; however, cookies for requests from other websites will never be sent. And the browser will keep sending it until the cookie expires. Cookie technology is a very controversial technology. Since its inception, it has become a focus of debate for the majority of Internet users and Web developers. Some Internet users, including some senior Web experts, are dissatisfied with its creation and promotion. This is not because the functionality of Cookie technology is too weak or for other technical performance reasons, but simply because they feel that the use of Cookies , causing harm to the privacy of network users. Because a cookie is a small text file saved on the user's browser by the Web server, it contains information about the user (such as an identification number, password, the way the user purchases on the Web site, or the number of times the user visits the site). So what exactly is Cookie technology? Does it really bring harm to the personal privacy of Internet users? After reading the above information, you should have a measure in mind.