jquery 怎样查看 cookie 时间

jquery 怎样查看 cookie 时间,第1张

jQuerycookie = function(name, value, options) {
if (typeof value != 'undefined') {
options = options || {};
if (value === null) {
value = '';
optionsexpires = -1;
}
var expires = '';
if (optionsexpires && (typeof optionsexpires == 'number' || optionsexpirestoUTCString)) {
var date;
if (typeof optionsexpires == 'number') {
date = new Date();
datesetTime(dategetTime() + (optionsexpires 24 60 60 1000));
} else {
date = optionsexpires;
}
expires = '; expires=' + datetoUTCString();
}
var path = optionspath '; path=' + (optionspath) : '';
var domain = optionsdomain '; domain=' + (optionsdomain) : '';
var secure = optionssecure '; secure' : '';
documentcookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure]join('');
} else {
var cookieValue = null;
if (documentcookie && documentcookie != '') {
var cookies = documentcookiesplit(';');
for (var i = 0; i < cookieslength; i++) {
var cookie = jQuerytrim(cookies[i]);
if (cookiesubstring(0, namelength + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookiesubstring(namelength + 1));
break;
}
}
}
return cookieValue;
}
}
$cookie(name, value, {expires, path, domain, secure}); // 创建COOKIE
$cookie(name); // 获取COOKIE

<%@ page contentType="text/html;charset=gb2312" %>
<%
String ipAddr=null;
Cookie cookies[] = requestgetCookies();
if (cookies != null) {
for (int i = 0; i < cookieslength; i++) {
if (cookies[i]getName()equals("ipAddr")){
ipAddr=cookies[i]getValue();
outprint("<script language=\"JavaScript\">alert(\"欢迎您再次访问本网页,上次登录IP地址为"+ipAddr+"\");</script>");
}
}
}
Cookie ipAddrCookie = new Cookie("ipAddr", "" + requestgetRemoteAddr());
ipAddrCookie setMaxAge(60 10); //设置Cookie有效期10分钟
responseaddCookie(ipAddrCookie );
outprint("<script language=\"JavaScript\">alert(\"欢迎您首次访问本网页,您的IP地址为"+requestgetRemoteAddr()+"\");</script>");

%>

③Expires 指定 cookie 的过期日期。为了在会话结束后将 cookie 存储在客户端磁盘上,或在许多时候,我们希望能更长时间地在访问者的计算机上保存cookie。必须设置该日期。若此项属性的设置未超过当前日期,则在任务结束后 cookie 将到期。
后面的代码,就可以设置cookie的使用到期时间为“2010年1月1日”: ResponseCookies("CookieName")Expires=#January 01, 2010#
后面的代码,将设定cookie的过期时间为“cookie的创建时间+365天”:ResponseCookies("CookieName")Expires=Date+365
但最好不要随便写 ResponseCookies

1. dateadd法
1)将期限设置为当前日期后的第N天的0时0分0秒
ResponseCookies(“LastView”)Expires=dateadd(“d”,N,date)
2)将期限设置为当前日期后的第N月的同一天的0时0分0秒
ResponseCookies(“LastView”)Expires=dateadd(“m”,N,date)
3)将期限设置到当前时间后的第N天的该时该分该秒
ResponseCookies(“LastView”)Expires=dateadd(“d”,N,now)
4)将期限设置到当前时间后的第N+M天的该时该分该秒
ResponseCookies(“LastView”)Expires=dateadd(“d”,N,now+M)
5)将期限设置到当前时间后的第N月的该时该分该秒
ResponseCookies(“LastView”)Expires=dateadd(“m”,N,now)
6)将期限设置到当前时间后的第N月的同一日后的第M天的该时该分该秒
ResponseCookies(“LastView”)Expires=dateadd(“m”,N,now+M)
2. 确定日期法
1)将期限设置到某一日的0时0分0秒失效
举例:到2003年1月29日0时0分0秒失效
ResponseCookies(“LastView”)Expires=# 1/29/2003 #

ResponseCookies(“LastView”)Expires=# 2003/1/29 #

ResponseCookies(“LastView”)Expires=“January 29,2003”
2)将期限设置到某一日的某一时刻失效
举例:到2003年1月29日21时0分0秒失效
ResponseCookies(“LastView”)Expires = #2003/1/29 21:00:00#

ResponseCookies(“LastView”)Expires = #1/29/2003 21:00:00#

ResponseCookies(“LastView”)Expires = #January 29,2003 21:00:00#
其实,在这种方法中,用““ ””和“# #”的效果是一样的。例如
ResponseCookies(“LastView”)Expires=“January 29,2003”

ResponseCookies(“LastView”)Expires=# January 29,2003 # 的效果就是相同的。
3. date+ \now+ 法
这种方法主要结构为Date+数学式或now +数学式。通过简单的四则运算的方法,将Cookie 的失效时间设置到当前时间后的某一段时间内。这里的加号都表示在当前时间的基础上加上预设时间。
1)date+法
如将期限设置为当前日期后的第N天的0时0分0秒,我们可以使用上述已谈到过的方法也可以使用如下格式:
ResponseCookies(“LastView”)Expires=Date+N
date+法是一种比较死的方法。它和上述方法差别不是很大,完全可以由上述几种方法代替。下面介绍now+法,这是一种比较灵活的方法,它可以将失效时间准确地定位到当前时间后的任意一年、一月、一天、一小时、一分、一秒。
2)now+法
先举一个例子: ResponseCookies(“LastView”)Expires = now+1
这条语句的功能是:把Cookie的失效时间限制到1天后的同一时刻。这里的1代表1天,即24小时。由于now代表当前Web服务器的系统时间,包括年、月、日、时、分、秒,则该语句所表达得Cookie 的失效日期在明天的同一时、分、秒上。若改成now+2则表示此Cookie 的失效日期在两天后的同一时、分、秒上;now+30表示此Cookie 的失效日期在一个月后的同一时、分、秒上;now+302表示此Cookie 的失效日期在两个月后的同一时、分、秒上;now+365则表示此Cookie 的失效日期在一年后的同一时、分、秒上……
当把1分成24等份,即该语句变成: ResponseCookies(“LastView”)Expires = now+1/24则表示把Cookie的失效时间限制到一小时后。在此基础上,把1再分60等份,即把now+1/24改成 now+1/1440,则表示把Cookie的失效时间限制到一分钟后。这里的1440是由2460得来。同理,要把Cookie的失效时间限制到十分钟后,则把程序变成ResponseCookies(“LastView”)Expires = now+10/1440;要把Cookie的失效时间限制到一秒钟后,则程序变成ResponseCookies(“LastView”)Expires = now+1/86400。当然,理论上把Cookie的失效时间限制到微秒级也是可以的。不过,那就没有什么实际意义了。另外,如果程序语句变成: ResponseCookies(“LastView”)Expires = now+1+1/1440则表示Cookie的失效时间为24小时零一分钟后。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13173168.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-15
下一篇 2023-06-15

发表评论

登录后才能评论

评论列表(0条)

保存