Home >Backend Development >PHP Tutorial >Introduction to PHP Cookie Technology_PHP Tutorial

Introduction to PHP Cookie Technology_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:23747browse

本文概要的讲解了Cookie的起源和根本属性,为学些PHP设置Cookie提供技术背景,推荐PHP新手一看。
在学习PHP如何设置Cookie之前我们先来了解下Cookie技术。
  (1)Cookie技术简介

   在WEB技术发展史上,Cookie技术的出现是一个重大的变革。最先是Netscape在它的Netscape Navigator浏览器中引入了Cookie技术,从那时起,World Wide Web 协会就开始支持Cookie标准。以后又经过微软的大
力推广(因为微软的IIS Web服务器所采用的ASP技术很大程度的使用了Cookie技术),即在微软的Internet Explorer浏览器中完全支持Cookie技术。到现在,绝大多数的浏览器都支持Cookie技术,或者至少兼容Cookie
技术的使用。
  1)什么是Cookie?
  按照Netscape官方文档中的定义,Cookie是在HTTP协议下,服务器或脚本可以维护客户工作站上信息的一种方式。Cookie 是由Web服务器保存在用户浏览器上的小文本文件,它可以包含有关用户的信息(如身份
识别号码、密码、用户在Web站点购物的方式或用户访问该站点的次数)。无论何时用户链接到服务器,Web站点都可以访问Cookie信息。
  通俗地讲,浏览器用一个或多个限定的文件来支持Cookie。这些文件在使用Windows操作系统的机器上叫做Cookie 文件,在Macintosh机器上叫做magic Cookie 文件,这些文件被网站用来在上面存储Cookie数据。
网站可以在这些Cookie 文件中插入信息,这样对有些网络用户就有些副作用。有些用户认为这造成了对个人隐私的侵犯,更糟的是,有些人认为Cookie是对个人空间的侵占,而且会对用户的计算机带来安全性的危害。
  目前有些Cookie 是临时的,另一些则是持续的。临时的Cookie只在浏览器上保存一段规定的时间,一旦超过规定的时间该Cookie就会被系统清除。例如在PHP中Cookie被用来跟踪用户进程直到用户离开网站。持续
的Cookie则保存在用户的Cookie文件中,下一次用户返回时,仍然可以对它进行调用。
  在Cookie文件中保存Cookie,一些用户会过分地认为这将带来很大的问题。主要是有些用户担心Cookie会跟踪用户网上冲浪的习惯,譬如用户喜爱到那些类型的站点、爱从事些什么活动等。害怕这种个人信息一
旦落入一些别有用心的家伙手中,那么个人也就可能成为一大堆广告垃圾的对象,甚至遭到意外的损害。不过,这种担心压根儿不会发生,因为网站以外的用户是无法跨过网站来获得Cookie信息的。所以想以这种目
的来应用Cookie是不可能的。不过,由于一些用户错误的理解以及“以讹传讹”,一些浏览器开发商别无选择,只好作出相识的响应(例如Netscape Navigator4.0和Internet Explorer3.0都提供了屏蔽Cookie 的选
项)。
  对Cookie技术期待了这么久的结果是,迫使许多浏览器开发商在它们的浏览器中提供了对Cookie的灵活性控制功能。例如,目前的两大主流浏览器Netscape Navigator 和 Internet Explorer是这样处理Cookie
的:Netscape Navigator4.0不但可以接受Cookie进行警告,而且还可以屏蔽掉Cookie;InternetExplorer3.0也可以屏蔽 Cookie,但在Internet Explorer4.0中就只能进行接受警告而没有提供屏蔽选项,不过在Internet
Explorer4.0之后的更新版本中又加入了屏蔽Cookie的功能选项。
  此外,很多最新的技术甚至已经可以在不能屏蔽Cookie的浏览器上进行Cookie的屏蔽了。例如,可以通过将Cookie文件设置成不同的类型来限制Cookie的使用。但是,非常不幸地是,要是你想完全屏蔽Cookie的
话,肯定会因此拒绝许多的站点页面。因为当今已经有许多Web站点开发人员爱上了Cookie技术的强大功能,例如Session对象的使用就离不开Cookie的支持。
  尽管今天仍有一些网络用户对于Cookie的争论乐此不倦,但是对于绝大多数的网络用户来说还是倾向于接受Cookie的。因此,我们尽可以放心地使用Cookie技术来开发我们的WEB页面。

  2)Cookie是怎样工作的?
  要了解Cookie,必不可少地要知道它的工作原理。一般来说,Cookie通过HTTP Headers从服务器端返回到浏览器上。首先,服务器端在响应中利用Set-Cookie header来创建一个Cookie ,然后,浏览器在它的请
求中通过Cookie header包含这个已经创建的Cookie,并且反它返回至服务器,从而完成浏览器的论证。

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 visitor's registered name is also "Michael Jordan". The attributes of the created cookie 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 on 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 has undergone the
URL encoding operation of the URLEncode method. 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, for requests from other websites, cookies 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 cookies are small text files saved on the user's browser by the web server, they contain information about the user (such as identification number, password, how the user purchases on the website 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.

http://www.bkjia.com/PHPjc/508332.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508332.htmlTechArticleThis article briefly explains the origin and fundamental properties of Cookies, and provides technical background for learning how to set cookies in PHP. PHP is recommended A newbie takes a look. Before learning how to set cookies in PHP, let’s first understand...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn