首页 > 其他 > 详细

freeecms项目中cookies的使用

时间:2014-05-15 21:33:23      阅读:535      评论:0      收藏:0      [点我收藏+]

cookie.js 代码:

bubuko.com,布布扣
function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));//unescape解码
}


function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))//escape编码
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}


function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}


function GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
} 
bubuko.com,布布扣

 

login.jsp中代码:

bubuko.com,布布扣
<div class="input_post">
    <input type="hidden" name="lt" value="_c3F041F78-53B9-69A5-3B3C-01BAB6E90925_k37408022-0986-F9AF-9DBB-8FBEC82968E9" />
    <input type="hidden" name="_eventId" value="submit" />
 
    <input class="button"   name="submit" accesskey="l" value="登 录" tabindex="4" type="submit" />
</div> <div class="input_fpwd"><input id="RememberMe" type="checkbox" name="RememberMe" onclick="RememberMeClick()"/>
  <label for="RememberMe">在此计算机上保留登录帐号</label>
</div>
bubuko.com,布布扣

 

login.jsp中js代码:

bubuko.com,布布扣
 <script>
            if(GetCookie("FreeCMS_loginName")!=null&&GetCookie("FreeCMS_loginName")!=""){
                document.getElementById("RememberMe").checked=true;
            }
            if(GetCookie("FreeCMS_loginName")!=null)document.getElementById("username").value=GetCookie("FreeCMS_loginName");
            function RememberMeClick(){
                if(document.getElementById("RememberMe").checked==false){
                    //删除cookie
                    DelCookie("FreeCMS_loginName");
                }
            }
            function submitForm(){
                
                if($.trim($("#username").val())==""){
                    alert("请输入用户名!");
                    $("#username").focus();
                    return false;
                }
                if($.trim($("#password").val())==""){
                    alert("请输入密码!");
                    $("#password").focus();
                    return false;
                }
                if($.trim($("#ValidateCode").val())==""){
                    alert("请输入验证码!");
                    $("#ValidateCode").focus();
                    return false;
                }
                if(document.getElementById("RememberMe").checked==true){
                    //设置cookie
                    SetCookie("FreeCMS_loginName",document.getElementById("username").value,60*60*24*365);
                }
                return true;
            }
            $("#username").focus();
            </script>
bubuko.com,布布扣


service中代码:

bubuko.com,布布扣
import javax.servlet.http.Cookie;

public
String login(){ try { //记住用户名 if("on".equals(RememberMe)){ Cookie cookie=new Cookie("FreeCMS_memberLoginName",EscapeUnescape.escape(member.getLoginname())); cookie.setMaxAge(1000*60*60*24*365);//有效时间为一年 getHttpResponse().addCookie(cookie); } HttpSession session =getHttpSession(); if (ValidateCode!=null && ValidateCode.equals(session.getAttribute("rand"))) { showMessage=memberService.checkLogin(getHttpSession(), member); }else { showMessage="验证码错误!"; } if (showMessage==null || "".equals(showMessage)) { OperLogUtil.log(member.getLoginname(), "会员登录", getHttpRequest()); getHttpResponse().sendRedirect("member/member_index.do"); return null; }else { return showMessage(showMessage, forwardUrl, forwardSeconds); } } catch (Exception e) { DBProException(e); OperLogUtil.log(member.getLoginname(), "会员登录失败:"+e.toString(), getHttpRequest()); return showMessage("出现错误:"+e.toString()+"", forwardUrl, forwardSeconds); } }
bubuko.com,布布扣

 

 

freeecms项目中cookies的使用,布布扣,bubuko.com

freeecms项目中cookies的使用

原文:http://www.cnblogs.com/kentyouyou/p/3729703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!