记录您上次访问时间

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>
记录您上次访问时间
</title>
<script language="JavaScript">
<!--
//定义设置Cookie的功能函数
function setCookie(name,value,expires,path,domain,secure)
{

  //首先判断Cookie名是否为空
  if(!name)
  {
    //如果为空,给出提示信息并返回
    alert("必须为cookie指定名字!");
    return;
  }
  //判断Cookie值是否为空
  if(value==null)
  {
    //如果为空,给出提示信息并返回
    alert("请为cookie指定一个值!");
    return;
  } 
  //定义一个变量用来存放整个Cookie串 
  var thecookie;
  //根据本函数的参数,构造Cookie串
  thecookie=name+"="+escape(value)+
                ((expires)?"; expires="+expires.toGMTString():"")+
                ((path)?";path="+path:"")+
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
  //判断Cookie是否过大
  if ((name + "=" + escape(value)).length <= 4000)
  {
     document.cookie=thecookie;
  }
  else{
     if(confirm("Cookie的大小超过了4KB,它将被截断!"))
     {
        document.cookie = thecookie;
     }
  }
}

//定义取得Cookie值的功能函数
function getCookie(name)
{
  var temp;
//首先取得Cookie名
  temp=name+"=";
  var start;
  //根据Cookie名,在整个Cookie串中搜索该Cookie
  start=document.cookie.indexOf(temp);
  //如果未找到
  if (start==-1)
  {
    //返回空值
    return null;
  }
  var end;
  //搜索该Cookie值在什么地方结束
  end=document.cookie.indexOf(";",start+temp.length)
  //如果没找到结束位置标记
  if(end==-1)
  {
    //返回整个Cookie串的长度
    end=document.cookie.length;
  }
var result;
  //从整个Cookie串中找出该Cookie的值
  result=document.cookie.substring(start+temp.length,end);
  //对该cookie值进行解码
  result=unescape(result);
  //返回该Cookie的值
  return result;
}

//定义删除Cookie的函数
function deleteCookie(name, path, domain)
{
  //首先判断该Cookie是否存在
  if (getCookie(name))
  {
    var thecookie;
    //构造一个与要删除的Cookie重名,但是立即过期的Cookie
    thecookie=name+"="+
              ((path) ? "; path=" + path : "") +
              ((domain) ? "; domain=" + domain : "") +
              "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    //让该Cookie起作用
    document.cookie=thecookie;
  }
  else{
    //如果该Cookie不存在,给出提示信息
    alert("该cookie并不存在!");
  }
}

//定义用于显示来访信息的功能函数
function display()
{
  //定义用来存放用户名和时间的变量
  var thename;
  var theyear;
  var themonth;
  var theday;
  var thehour;
  var theminute;
  var thesecond;
  //取得Cookie中的用户名
  thename=getCookie("username");
  //判断该Cookie是否存在
if(!thename)
  {
    //如果不存在,表明该用户是第一次登陆本页面
    var thename1;
    //让用户输入登陆名
    thename1=prompt("请告诉我您的姓名:","");
    //如果输入不合法,则一直让他输
    while((thename1=="")||(thename1==null))
    {
      thename1=prompt("不告诉我您的姓名,您可进不去呀!","");
    }
    var now;
    //取的当前时间
    now=new Date();
    //取得当前时间的各项值
    theyear=now.getYear();
    if(theyear<2000)
    {
      theyear+=1900;
    }
    themonth=now.getMonth();
    themonth+=1;
    theday=now.getDate();
    thehour=now.getHours();
    theminute=now.getMinutes();
    thesecond=now.getSeconds();
    //得到一年后的时间
    now.setTime(now.getTime()+365*24*60*60*1000);
    //设置包含用户名的Cookie值  
    setCookie("username",thename1,now);   
    //设置包含年份的Cookie值 
    setCookie("preyear",theyear,now);
    //设置包含月份的Cookie值  
    setCookie("premonth",themonth,now);
    //设置包含几号的Cookie值  
    setCookie("preday",theday,now);
    //设置包含小时数的Cookie值  
    setCookie("prehour",thehour,now);
    //设置包含分钟数的Cookie值  
    setCookie("preminute",theminute,now);
    //设置包含秒数的Cookie值 
    setCookie("presecond",thesecond,now);
    //按相应的格式显示用户名及上次登陆时间
    document.write("<p align='center'>");
    document.write("<font face='隶书' size='4' color='red'>");
    document.write(thename1+" 您好! 您这是第一次访问本站点.<br>今后请常来坐坐呀!");
    document.write("</font>");
    document.write("</p>");
  }
  else{
    //取的用户上次登陆时间的年份
    theyear=getCookie("preyear");
    //取的用户上次登陆时间的月份
    themonth=getCookie("premonth");
    //取的用户上次登陆时间的号数
    theday=getCookie("preday");
    //取的用户上次登陆时间的小时数
    thehour=getCookie("prehour");
    //取的用户上次登陆时间的分钟数
    theminute=getCookie("preminute");
    //取的用户上次登陆时间的秒数
    thesecond=getCookie("presecond");
    document.write("<p align='center'>");
    document.write("<font face='隶书' size='4' color='red'>");
    document.write(thename+" 您好!<br>您上次访问本站点的时间是<br>"
    +theyear+"年"+themonth+"月"+theday+"日"+thehour+":"+theminute
    +":"+thesecond);
    document.write("</font>");
    document.write("</p>");
    var now;
    //取得当前时间
    now=new Date();
    //取得当前时间的各项值
    theyear=now.getYear();
    if(theyear<2000)
    {
      theyear+=1900;
    }
    themonth=now.getMonth();
    themonth+=1;
    theday=now.getDate();
    thehour=now.getHours();
    theminute=now.getMinutes();
    thesecond=now.getSeconds();
    //得到一年后的时间
    now.setTime(now.getTime()+365*24*60*60*1000);
    //设置包含用户名的Cookie值
    setCookie("username",thename,now);
    //设置包含年份的Cookie值
    setCookie("preyear",theyear,now);
    //设置包含月份的Cookie值
    setCookie("premonth",themonth,now);
    //设置包含几号的Cookie值
    setCookie("preday",theday,now);
    //设置包含小时数的Cookie值
    setCookie("prehour",thehour,now);
    //设置包含分钟数的Cookie值
    setCookie("preminute",theminute,now);
    //设置包含秒数的Cookie值
    setCookie("presecond",thesecond,now);
  }
}
// -->
</script>

</head>
<body>
<p align="center">
<font face="金山简行楷" size="5" color="blue">
亲爱的网友
<br>
本站很高兴能认识你!
</font>
</p>
<script language=javascript>
<!--
//调用功能函数显示用户及上次登录时间
display();
// -->
</script>

</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值