zoukankan      html  css  js  c++  java
  • Cookie的实用随记

    这里使用的是Ajax 和一般处理文件进行的操作。

    简单描述,一个用户登录进去,则创建Cookie ,当用户退出则去除Cookie,

    JS 代码如下:

    $("#submit").click(function () { if (check_email() == true && check_psw() == true) {// 判断是否验证成功 $.post( '/ashx/Dd_do_login.ashx', { email: $("#mail").val(), password: $("#password").val() }, function (data) { var arryuser = data.split(','); if (arryuser[0] == '11') { jq.ajax({ type: "post", url: '/ashx/Dd_userExit.ashx', data: { type: "login", user_ID: arryuser[1], user_Name: arryuser[2], user_Email: $("#mail").val() }, success: function (data) { window.location.href = "/Default.aspx"; } }); } else if (arryuser[0] == "01") { alert("不好意思,你的账号还没有激活,请登录邮箱进行账号激活!"); } else { $("#error_div").show(); $("#error_mess").text("邮箱或密码不正确!"); return false; } } ); } else { return false; } }); });    )
    
    
    一般处理文件:
    用户登录:
     public class Dd_do_login : IHttpHandler     {         BlogDll.Dd_User bll = new BlogDll.Dd_User();         Model.Dd_User model = new Model.Dd_User();         public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "text/plain";             string email = context.Request.Params["email"];             string pwd = context.Request.Params["password"];             string encryPwd = Encrypt.EncryptPassword(pwd, "MD5");             StringWriter sw = new StringWriter();             if (Exists(email, pwd))             {                 if (UserIsActive(email))                 {                     sw.Write("11" + "," + GetUserName(email));//登陆成功且号码已激活                 }                 else                 {                     sw.Write("01" + "," + "Error");//表示未激活状态                 }             }             else             {                 sw.Write("00" + "," + "Error");//用户名或密码错误。             }             HtmlTextWriter htw = new HtmlTextWriter(sw);             context.Response.Write(sw.ToString());             context.Response.End();         }         /// <summary>         /// 判断是否存在用户         /// </summary>         /// <param name="email">用户Email</param>         /// <param name="pwd">用户密码</param>         /// <returns></returns>         public bool Exists(string email, string pwd)         {             StringBuilder sql = new StringBuilder();             sql.Append("SELECT COUNT(1) FROM Dd_User");             sql.Append("WHERE User_Email=@Eamil  AND  User_Passwd=@pwd");             if (!String.IsNullOrEmpty(email) && !String.IsNullOrEmpty(pwd))             {                 SqlParameter[] parame ={                                       new SqlParameter("@User_Email",email),                                        new SqlParameter("@User_Password",pwd)                                   };                 return DbHelperSQL.GetExists(sql.ToString());             }             else             {                 return false;             }         }         /// <summary>         /// 根据Email 获取用户名         /// </summary>         /// <param name="email">用户Email</param>         /// <returns></returns>         public string GetUserName(string email)         {             var userName = String.Empty;             var sql = "select User_ID,User_BlogName from Dd_User where User_Email='" + email + "'";             SqlDataReader dr = DbHelperSQL.ExecuteReader(sql);             while (dr.Read())             {                 userName += dr["User_ID"].ToString() + "," + dr["User_BlogName"];             }             return userName;         }         /// <summary>         /// 判断用户是否激活         /// </summary>         /// <param name="email"></param>         /// <returns></returns>         public bool UserIsActive(string email)         {             var sql = "select User_ActivCondition from Dd_User where User_Email=@email";             SqlParameter[] parame ={                                         new SqlParameter("@email",email)                                     };             int state = int.Parse(DbHelperSQL.GetSingle(sql, parame).ToString());             if (state == 1)             {                 return true;             }             else             {                 return false;             }         }         public bool IsReusable         {             get             {                 return false;             }         }     }
    退出:
    public class Dd_userExit : IHttpHandler     {                 public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "text/plain";             if (context.Request.Params["type"] == "login")//获取并判断Ajax 请求类型             {               var userID=context.Request.Params["user_ID"].ToString();//获取参数               var name = context.Request.Params["user_Name"].ToString();               var email = context.Request.Params["user_Email"].ToString();               HttpCookie cookie = new HttpCookie("login");//创建名为“login”的Cookie               cookie.Values.Add("user_ID", userID);//赋值               cookie.Values.Add("user_Name", name);               cookie.Values.Add("user_Email", email);               cookie.Expires = DateTime.Now.AddDays(7);//设置过期时间               context.Response.Cookies.Add(cookie);               context.Session["user_Message"] = userID + "," + name + "," + email;//Session 赋值               context.Response.Write("1");             }             else if (context.Request.Params["type"] == "exit")//如果是退出 则执行删除Cookie操作。             {                 HttpCookie hCookie = context.Request.Cookies["login"];                 hCookie.Value = null;                 context.Response.Cookies.Add(hCookie);                 context.Session.Remove("user_Message");                 context.Response.Write("1");             }         }         public bool IsReusable         {             get             {                 return false;             }         }     }
     
  • 相关阅读:
    git命令
    Linux基础知识手册
    Linux系统编程
    A
    Subsequences in Substrings Kattis
    G
    K
    K
    C
    E
  • 原文地址:https://www.cnblogs.com/BungeeJumping/p/2343872.html
Copyright © 2011-2022 走看看