Introduction to PHP Cookie Technology

Source: Internet
Author: User
Keywords Introduction to PHP Cookie Technology

This article provides an overview of the origins and fundamental attributes of cookies, providing a technical background for learning PHP settings cookies, and recommending a novice PHP look.
Before learning how to set up cookies for PHP, let's look at the cookie technology.
(1) Introduction to Cookie Technology

In the history of Web technology, the advent of cookie technology is a major change. First, Netscape introduced Cookie technology in its Netscape Navigator browser, and since then the world Wide Web Society has started to support cookie standards. After Microsoft's big
(Because Microsoft's IIS Web server uses ASP technology to a large extent using cookie technology), which is fully supported in Microsoft's Internet Explorer browser. Up to now, most browsers support cookie technology, or at least cookie-compatible
The use of technology.
1) What is a cookie?
As defined in the official Netscape document, a cookie is a way for a server or script to maintain information on a client workstation under the HTTP protocol. A Cookie is a small text file that is saved by a Web server on a user's browser and can contain information about a user, such as an identity
Identify the number, password, how the user shopped on the Web site, or how many times the user visited the site. The Web site can access cookie information whenever a user is linked to a server.
In layman's words, browsers use one or more qualified files to support cookies. These files are called Cookie files on machines that use the Windows operating system and are called Magic Cookie files on Macintosh machines that are used by the Web site to store cookie data on it.
Web sites can insert information into these cookie files, which may have some side effects on some web users. Some users believe that this has caused a violation of personal privacy, and worse, some people think that the cookie is the occupation of personal space, but also the user's computer to bring security hazards.
Some cookies are temporary and others are ongoing. A temporary cookie is only stored on the browser for a specified period of time, and the cookie will be purged by the system once the specified time has passed. In PHP, for example, cookies are used to track user processes until the user leaves the site. Continued
Cookie is stored in the user's cookie file, and the next time the user returns, it can still be called.
Cookies are stored in cookie files, and some users are overly likely to think that this poses a big problem. The main concern is that cookies keep track of users ' habits of surfing the web, such as what types of sites they love, what activities they like to engage in. Afraid of this personal information one
Once falling into the hands of some ulterior motives, the individual can become a large pile of advertising garbage objects, and even suffered accidental damage. However, this fear does not occur at all, because users outside the site are unable to cross the site to obtain cookie information. So I want to use this kind of purpose
To apply the cookie is not possible. However, due to some user error understanding and "baseless assertion", some browser developers have no choice but to make an acquaintance response (such as Netscape Navigator4.0 and Internet Explorer3.0 have provided a screening cookie
Items).
The long-awaited result of Cookie technology has forced many browser developers to provide the flexibility to control cookies in their browsers. For example, the current two major browsers Netscape Navigator and Internet Explorer handle cookies like this
: Netscape Navigator4.0 can not only accept cookies for warning, but also can block out cookie;internetexplorer3.0 cookies, but on the internet You can only accept warnings in Explorer4.0 and do not provide masking options, but the internet
The feature options for masking cookies are added to the updated version after Explorer4.0.
In addition, many of the latest technologies have even been able to block cookies on browsers that cannot block cookies. For example, you can limit the use of cookies by setting the cookie file to a different type. But, very unfortunately, if you want to completely block the cookie
, it will certainly reject many of the site pages. Because many web site developers today have fallen in love with the powerful features of cookie technology, such as the use of Session objects without the support of cookies.
Although there are still some Internet users who are enjoys about the cookie debate today, they tend to accept cookies for the vast majority of Internet users. As a result, we can safely use cookie technology to develop our web pages.

2) How does a cookie work?
To understand cookies, it is essential to know how it works. In general, cookies are returned from the server side to the browser via HTTP headers. First, the server uses the Set-cookie header in the response to create a Cookie, and then the browser
The cookie header contains the cookie that has been created and returns it to the server to complete the browser's argument.

For example, we created a cookie with the name login to contain the visitor's information, and when the cookie was created, the server-side header looks like this, assuming that the visitor's registered name is "Michael Jordan", Also, the attributes of the cookie created, such as Path,
Domain, expires, etc. are specified.

Set-cookie:login=michael jordan;path=/;d omain=msn.com;
expires=monday,01-mar-99 00:00:01 GMT

The header above automatically adds a record to the cookie file of the browser-side computer. The browser assigns a cookie with the variable named "Login" to "Michael Jordon". Note that the value of this cookie is passed through the UrlEncode method during the actual delivery process.
The URL encoding operation. After the HTTP header with the cookie value is saved to the cookie file in the browser, the header notifies the browser to return the cookie to the server via a request to ignore the path, completing the browser's authentication operation.

In addition, we use some of the properties of the cookie to limit the use of the cookie. For example, the domain attribute is able to restrict the sending of cookies on the browser side, in the case of 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 property specifies the time period for which the cookie is saved, such as the cookie above is only saved to March 1, 1999 1 seconds on the browser. Of course, if there are too many cookies on the browser that exceed the allowed range of the system, the browser will automatically
it for deletion. As for the attribute path, specify which directory path the cookie will be sent to the server.

Note: After a cookie has been created by the browser, the cookie is carried in the header for each request for the site, but the request cookie for other sites is never followed. and the browser will continue to send this until the cookie expires. Cookie technology is a very controversial technology, since the birth of it has become a vast number of web users and web developers of a controversial focus. Some web users, even some experienced web experts, are also dissatisfied with its emergence and promotion, not because the function of cookie technology is too weak or other technical performance reasons, but simply because they feel that the use of cookies, the privacy of users of the network is a threat. Because a cookie is a small text file that is saved by the Web server on the user's browser, it contains information about the user (such as identity
Identify the number, password, how the user shopped on the Web site, or how many times the user visited the site. So what about cookie technology? Does it really bring personal privacy to the Internet users? Look at the above information in your mind should have a measure of it.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.