Cookie只能通过特定的URL访问 [英] Cookie accessible only by specific URL

查看:60
本文介绍了Cookie只能通过特定的URL访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript获取和设置cookie,但只希望对其进行设置的页面可以访问它们(即cookie对页面是私有的,因此没有其他页面可以干扰或读取它们).

I want to get and set the cookie using javascript, but want them only to be accessible to the page that set them (i.e the cookie is private to the page, so no other pages can interfere or read them).

推荐答案

这是您需要做的:

假设您在页面上,网址为 http://www.example.com/pages/myPage.html ,并且仅在设置/创建您拥有的Cookie时,您才想限制对此特定页面( myPage.html )上Cookie的访问设置路径参数当前页面相对路径.

Suppose you are on the page with url http://www.example.com/pages/myPage.html, and you want to limit the access to cookies on this particular page (myPage.html) only then while setting/creating the cookie you have the set the path parameter as current page relative path.

var pathToMyPage = window.location.pathname; // this gives pages/myPage.html
document.cookie('name=value;path='+ pathToMyPage); 

现在,如果您尝试在其他页面的cookie中查找此特定键,请说/pages/myPage2.html .您不会在那找到它.

Now if you try to look for this particular key in the cookie of other pages say /pages/myPage2.html. You won't find it there.

这篇关于Cookie只能通过特定的URL访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆