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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:58:52  来源:igfitidea点击:

How to get cookie expiration date / creation date from javascript?

javascriptcookies

提问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

It's now possible with new chrome update for version 47 for 2016 , you can see it through developer tools on the resources tabenter image description here, select cookies and look for your cookie expiration date under "Expires/Max-age"

现在可以使用 2016 年版本 47 的新 chrome 更新,您可以通过资源选项卡上的开发人员工具查看它在此处输入图片说明,选择 cookie 并在“过期/最大年龄”下查找您的 cookie 到期日期

回答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 列表及其到期日期。