Home  >  Article  >  Backend Development  >  A must-read for newbies: The difference between session and cookie_PHP Tutorial

A must-read for newbies: The difference between session and cookie_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:131125browse

session和cookie是网站浏览中较为常见的两个概念,也是比较难以辨析的两个概念,但它们在点击流及基于用户浏览行为的网站分析中却相当关键。基于网上一些文章和资料的参阅,及作者个人的应用体会,对这两个概念做一个简单的阐述和辨析,希望能与大家共同探讨下。

session和cookie的最大区别在于session是保存在服务端的内存里面,而cookie保存于浏览器或客户端文件里面;session是基于访问的进程,记录了一个访问的开始到结束,当浏览器或进程关闭之后,session也就“消失”了,而cookie更多地被用于标识用户,它可以是长久的,用于用户跟踪和识别唯一用户(Unique Visitor)。

关于session

session被用于表示一个持续的连接状态,在网站访问中一般指代客户端浏览器的进程从开启到结束的过程。session其实就是网站分析的访问(visits)度量,表示一个访问的过程。

session的常见实现形式是会话cookie(session cookie),即未设置过期时间的cookie,这个cookie的默认生命周期为浏览器会话期间,只要关闭浏览器窗口,cookie就消失了。实现机制是当用户发起一个请求的时候,服务器会检查该请求中是否包含sessionid,如果未包含,则系统会创造一个名为JSESSIONID的输出 cookie返回给浏览器(只放入内存,并不存在硬盘中),并将其以HashTable的形式写到服务器的内存里面;当已经包含sessionid是,服务端会检查找到与该session相匹配的信息,如果存在则直接使用该sessionid,若不存在则重新生成新的 session。这里需要注意的是session始终是有服务端创建的,并非浏览器自己生成的。

但是浏览器的cookie被禁止后session就需要用get方法的URL重写的机制或使用POST方法提交隐藏表单的形式来实现。

这里有一个很关键性的注意点,即session失效时间的设置,这里要分两方面来看:浏览器端和服务端。对于浏览器端而言,session与访问进程直接相关,当浏览器被关闭时,session也随之消失;而服务器端的session失效时间一般是人为设置的,目的是能定期地释放内存空间,减小服务器压力,一般的设置为当会话处于非活动状态达20或30分钟时清除该 session,所以浏览器端和服务端的session并非同时消失的,session的中断也并不一定意味着用户一定离开了该网站。目前Google Analytics和Omniture都定义当间隔30分钟没有动作时,算作一次访问结束,所以上图中session的最后一步不只是离开,也有可能是静止、休眠或者发呆的状态。

还有一点需要注意,就是现在的浏览器好像趋向于多进程的session共享,即通过多个标签或页面打开多个进程访问同一网站时共享一个 session cookie,只有当浏览器被关闭时才会被清除,也就是你有可能在标签中关闭了该网站,但只要浏览器未被关闭并且在服务器端的session未失效前重新开启该网站,那么就还是使用原session进行浏览;而某些浏览器在打开多页面时也可能建立独立的session,IE8、Chrome默认都是共享 session的,在IE8中可以通过菜单栏中的文件->新建会话来建立独立session的浏览页面。

关于cookie 

cookie 是一小段文本信息,伴随着用户请求和页面在Web服务器和浏览器之间传递。用户每次访问站点时,Web应用程序都可以读取cookie包含的信息。

The common method that has been introduced in the session implementation mechanism is to use session cookies, and the usual cookies mainly refer to another type of cookie - persistent cookies. Persistent cookies refer to cookie information stored on the client's hard drive (set with a certain validity period). When a user visits a website, the browser will search for it on the local hard drive. Cookies associated with this website. If the cookie exists, the browser will send it to your site through the HTTP header information together with the page request, and then the system will compare the attributes and values ​​in the cookie to see if they are consistent with the information stored on the server , and determine the user as a "first time visitor" or "old customer" based on the comparison results.

Persistent cookies generally save the user's user ID. This information is generated by the server when the user registers or logs in for the first time. A cookie containing the domain name and related information is sent and stored on the client's hard disk file. , and set the cookie expiration time to facilitate automatic user login and website content customization.

The mod_usertrack module that comes with Apache can plant a unique cookie (expires for a long time) to the user when the user first comes to the current website . This cookie is the first time the user comes to the current The IP address of the website plus a random string. At the same time, adding the %{cookie}n field at the end of the custom WEB log can realize the output of cookies in the apache log for data statistics and user tracking.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328164.htmlTechArticleSession and cookie are two common concepts in website browsing, and they are also two concepts that are difficult to distinguish, but They are quite similar in click stream and website analysis based on user browsing behavior...
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