网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Cookie的理解-创新互联

1》cookie详解


什么是cookie

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with the next request to the same server. Typically, it's used to tell if two requests came from the same browser — keeping a user logged-in, for example. It remembers stateful information for the stateless HTTP protocol.

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了云浮免费建站欢迎大家使用!

     cookie(“小甜饼”)就是服务器发送到我们web浏览器的一小块数据。浏览器会存储它并且当下次再访问同一个网站,web服务器会先看看它上次有没有他上次留下的cookies资料,有的话会依据cookie里的内容来判断使用者,送出特定的网页内容给你。


cookie的三大作用

-Session management
    logins,shopping carts,game scores,or anything else the server should remember

-Personalization(个性化设置)
    User preferences,themes,and other settings

-Tracking
    Recording and analyzing user behavior


Create Cookies (创建cookie)

When receiving an HTTP request, a server can send a Set-Cookie header with the response. The cookie is usually stored by the browser, and then the cookie is sent with requests made to the same server inside a Cookie HTTP header. An expiration date or duration can be specified, after which the cookie is no longer sent. Additionally, restrictions to a specific domain and path can be set, limiting where the cookie is sent.

    当服务器接收到一个http请求时会发送一个Set-Cookie的响应头,这cookie通常会被浏览器存储,并且这cookie会被写在访问同一个服务器的http请求头中。此外,我们使用了cookie的一些属性来限定cookie的使用。例如Domain属性能够在浏览器端对cookie发送进行限定;Expires属性则指定了该Cookie保存的时间限制;属性Path,则用来指定Cookie将被发送到服务器的哪一个目录路径下。

The Set-Cookie and Cookie headers

Set-Cookie 的响应头会从服务器端发送cookie到浏览器端。

HTTP/1.0 200 OK
Content-type: text/html
Set-Cookie: yummy_cookie=choco
Set-Cookie: tasty_cookie=strawberry

[page content]

现在每个新的请求,浏览器会利用cookie 头把所有先前存储的cookies发送到服务器。

GET /sample_page.html HTTP/1.1
Host: www.example.org
Cookie: yummy_cookie=choco; tasty_cookie=strawberry

Session cookie

上面的cookie是一个session cookie:当客户端关闭时它会被删除。因为它没有指定Expires或Max-Age指令。但是,Web浏览器可能会使用会话还原,这会使大多数会话cookie永久保留,就像浏览器从未关闭一样。

Permanent cookie

永久cookie只会在特定的时间过期而不是在客户端关闭时过期

Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT;

Secure and HttpOnly cookies

安全cookie仅通过HTTPS协议通过加密请求发送到服务器。但是尽管安全,重要的东西还是不能放在cookie中。为了防止跨站点脚本(XSS)***,JavaScript的Document.cookie API无法访问HttpOnly cookie;它们只被发送到服务器。

Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly

Scope of cookies(cookies 范围)

Domain和Path指令定义cookie的范围:cookie应发送到哪些URL。
Domain指定允许的主机接收cookie。如果未指定,则默认为当前文档位置的主机,不包括子域。如果指定了域,则始终包含子域。
例如,如果设置了Domain = mozilla.org,则cookie将包含在developer.mozilla.org等子域中。
Path表示在请求的URL中必须存在的URL路径,以便发送Cookie标头。 %x2F(“/”)字符被视为目录分隔符,子目录也将匹配。
For example, if Path=/docs is set, these paths will match:

/docs
/docs/Web/
/docs/Web/HTTP

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:Cookie的理解-创新互联
URL标题:http://bjjierui.cn/article/diohgj.html

其他资讯