Javascript 如何从javascript获取cookie到期日期/创建日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3274875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get cookie expiration date / creation date from javascript?
提问by Nir
Is it possible to retrieve the creation or expiration date of an existing cookie from javascript? If so how?
是否可以从 javascript 中检索现有 cookie 的创建日期或到期日期?如果是这样怎么办?
采纳答案by Victor Welling
The information is not available through document.cookie, but if you're really desperate for it, you could try performing a request through the XmlHttpRequest object to the current page and access the cookie header using getResponseHeader().
该信息无法通过 document.cookie 获得,但如果您真的非常渴望它,您可以尝试通过 XmlHttpRequest 对象向当前页面执行请求,并使用 getResponseHeader() 访问 cookie 标头。
回答by Pavel Strakhov
It's impossible. document.cookiecontains information in string like this:
不可能。document.cookie包含这样的字符串信息:
key1=value1;key2=value2;...
So there isn't any information about dates.
所以没有关于日期的任何信息。
You can store these dates in separate cookie variable:
您可以将这些日期存储在单独的 cookie 变量中:
auth_user=Riateche;auth_expire=01/01/2012
But user can change this variable.
但用户可以更改此变量。
回答by Yazan Rawashdeh
回答by bassem ala
you can't get the expiration date of a cookie through javascript because when you try to read the cookie from javascript the document.cookie return just the name and the value of the cookie as pairs
您无法通过 javascript 获取 cookie 的到期日期,因为当您尝试从 javascript 读取 cookie 时,document.cookie 仅返回 cookie 的名称和值对
回答by Pedro Sousa
Yes, It is possible. I've separated the code in two files:
对的,这是可能的。我已将代码分成两个文件:
index.php
索引.php
<?php
$time = time()+(60*60*24*10);
$timeMemo = (string)$time;
setcookie("cookie", "" . $timeMemo . "", $time);
?>
<html>
<head>
<title>
Get cookie expiration date from JS
</title>
<script type="text/javascript">
function cookieExpirationDate(){
var infodiv = document.getElementById("info");
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
}else{
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function (){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
infodiv.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "cookie.php", true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="button" onclick="javascript:cookieExpirationDate();" value="Get Cookie expire date" />
<hr />
<div id="info">
</div>
</body>
</html>
cookie.php
cookie.php
<?php
function secToDays($sec){
return ($sec / 60 / 60 / 24);
}
if(isset($_COOKIE['cookie'])){
if(round(secToDays((intval($_COOKIE['cookie']) - time())),1) < 1){
echo "Cookie will expire today";
}else{
echo "Cookie will expire in " . round(secToDays((intval($_COOKIE['cookie']) - time())),1) . " day(s)";
}
}else{
echo "Cookie not set...";
}
?>
Now, index.php must be loaded once. The button "Get Cookie expire date", thru an AJAX request, will always get you an updated "time left" for cookie expiration, in this case in days.
现在,index.php 必须加载一次。通过 AJAX 请求,“获取 Cookie 过期日期”按钮将始终为您提供 cookie 过期的更新“剩余时间”,在这种情况下以天为单位。
回答by inorganik
One possibility is to delete to cookie you are looking for the expiration date from and rewrite it. Then you'll know the expiration date.
一种可能性是删除您正在寻找到期日期的 cookie 并重写它。然后你就知道保质期了。
回答by skribbz14
If you are using Chrome you can goto the "Resources" tab and find the item "Cookies" in the left sidebar. From there select the domain you are checking the set cookie for and it will give you a list of cookies associated with that domain, along with their expiration date.
如果您使用的是 Chrome,您可以转到“资源”选项卡并在左侧边栏中找到“Cookies”项。从那里选择您正在检查设置 cookie 的域,它将为您提供与该域关联的 cookie 列表及其到期日期。


