1、申 请 I D:jucire
2、个人邮箱:b46202@outlook.com
3、原创技术文章:
使用Cookie登录,可以防止用户直接访问页面,需要登录。用于设置用户的登录和显示访问次数。
[JavaScript] 纯文本查看 复制代码 <script type="text/javascript">
var shijian = new Date(); var expiry = new Date(shijian.getTime() + 365 * 24 * 60 * 60 * 1000); function getCookieVal (offset) {
var over = document.cookie.indexOf (";", offset);
if (over == -1) { over = document.cookie.length; }
return unescape(document.cookie.substring(offset, over)); }
function test() {
var un = document.getElementById("dum");
if(un == null){
alert("无法删除该项.\n");
}
var uns = document.getElementById("dumdiv");
if(uns == null){
alert("无法删除该项.\n");
} } document.onload="test()";
function GetCookie (name)
{
var a = name + "=";
var alen = a.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == a) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null; } function DeleteCookie (name,path,domain)
{
if (GetCookie(name)) {
document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? ";
domain=" + domain : "") + "; expires=Mon, 01-Jan-18 00:00:00 GMT";
} } function SetCookie (name,value,expires,path,domain,secure)
{
document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); }
if (GetCookie('user_id') == null)
{
var getName = prompt('欢迎,请输入用户名', '');
var answer = confirm ('你好 ' + getName + '. 是否保存密码?')
if (answer)
{
SetCookie('user_id', (getName != '' ? getName : 'Anonymous user'), expiry);
SetCookie('hit_count', '1', expiry);
} document.writeln('<H1>欢迎, ' + (getName != '' ? getName : 'Anonymous user') + '</H1>' + '欢迎访问'); } else
{
var getName = GetCookie('user_id');
var getHits = GetCookie('hit_count');
getHits = parseInt(getHits) + 1;
document.writeln('<H1>欢迎回来, ' + getName + '</H1>' + '你来过 ' + getHits + ' times.');
SetCookie('hit_count', '' + getHits + '', expiry); } </script> |