日期:2014-05-18  浏览次数:20682 次

cookies记录登录次问题!
我网站用了cookies记录登录次数!
首页index.jsp的cookies是这样写的:
<%
int   counter=0;
Cookie   cookies[]=request.getCookies();
if(cookies!=null){
      for(int   i=0;i <cookies.length;i++){
              if(cookies[i].getName().equals( "counter "))
              counter=Integer.parseInt(cookies[i].getValue())+1;
    }
}
Cookie   c=new   Cookie( "counter ", " "+counter);
c.setMaxAge(60*60*12);
response.addCookie(c);
%>
<!--显示登录次数-->
<%
  if(counter==0){
        out.print( "欢迎首次光临! ");
}
else{
        out.print( "今天您已经光临了 "+counter+ "次! ");
        }
%>

其它页面other.jsp就这样写:
<%
int   counter=0;
Cookie   cookies[]=request.getCookies();
if(cookies!=null){
      for(int   i=0;i <cookies.length;i++){
              if(cookies[i].getName().equals( "counter "))
              counter=Integer.parseInt(cookies[i].getValue());
    }
}
%>  
<!--显示登录次数-->
<%
        if(counter==0){
        out.print( "欢迎首次光临! ");
}
else{
        out.print( "今天您已经光临了 "+counter+ "次! ");
        }
%>

问题就是这样:首页、其它页的登录次数不同步,特别是关了浏览器再打开时,首页跟其它页更是不同!怎么回事?用什么方法解决????
你们还有什么更好的记录登录次的程序吗?分享一下好不好^_^

------解决方案--------------------
用数据库表吧