How to get cookie expiration date / creation date from javascript?
JavascriptCookiesJavascript Problem Overview
Is it possible to retrieve the creation or expiration date of an existing cookie from javascript? If so how?
Javascript Solutions
Solution 1 - Javascript
It's impossible. document.cookie
contains information in string like this:
key1=value1;key2=value2;...
So there isn't any information about dates.
You can store these dates in separate cookie variable:
auth_user=Riateche;auth_expire=01/01/2012
But user can change this variable.
Solution 2 - Javascript
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().
Solution 3 - Javascript
It's now possible with new chrome update for version 47 for 2016 , you can see it through developer tools on Storage > Cookies under the Application tab , select cookies and look for your cookie expiration date under "Expires/Max-age"
Solution 4 - Javascript
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
Solution 5 - Javascript
Yes, It is possible. I've separated the code in two files:
index.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
<?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.
Solution 6 - Javascript
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.
Solution 7 - Javascript
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.