yoguz
查看yoguz的博客
| 金钱 | : 18255 |
| Level | : 0 |
| 发帖数 | : 1564 |
| 最后登陆 | : 2019/10/24 |
| 注册时间 | : 2004/12/13 |
|
asp中cookie使用示例 Expires Expires 属性指定了在浏览器上缓冲存储的页距过期还有多少时间。如果用户在某个页过期之前又回到此页,就会显示缓冲区中的版本。 语法 Response.Expires [= number] 参数 number 距过期还有多少分钟。将此参数设置为 0 可使缓存的页立即过期。 注释 若此属性在一页上设置了多次,则使用最短的时间。 设置cookies有效期为一小时: Response.Cookies("ssss").expires=dateadd("h",1,now()) 在客户端,Cookies记录了客户端浏览器很多的信息, 我们可通过Request.Cookies("名称")命令获取它的值, 也可通过Response.Cookies("名称")="值"在客户端记录一些信息,以便控制访问者。 设置多重Cookies的方法为Response.Cookies("名称")("名称")="值"。 ---------------- Response.Cookies 用于设置Cookies的值,语法为: Response.cookies(变量名称)[(key)|属性]=写入的信息。(key的用法前面已作说明,这里不在叙述) cookies系统默认有五个属性, 包括Expires(时间)、 Domain(域名)、 Path(路径)、 Secure(安全性)、 Haskeys(判断cookies下是否还有其它cookies文本)。 首先是前台的表单域: <form name="Login" action="loginaction.asp" method="post"> 用户名称 <input name="User" type="text" id="User" size="23" maxlength="20"> 用户密码:<input name="Password" type="password" id="Password" size="23" maxlength="20"> Cookie选项: <select name=CookieDate> <option selected value=0>不保存</option> <option value=1>保存一天</option> <option value=2>保存一月</option> <option value=3>保存一年</option> </select> <input type="submit" name="Submit" value=" 确认 "> <input name="reset" type="reset" id="reset" value=" 清除 "> </form> 验证页面 rs("szd_last_time")=now() rs("szd_cookie")=cookiedate rs.update jibie=rs("szd_jibie") call SaveCookie_shop() sub SaveCookie_shop() Response.Cookies("shop")("User")=user Response.Cookies("shop")("Password") = PassWord Response.Cookies("shop")("CookieDate") = CookieDate select case CookieDate case 0 ''not save case 1 Response.Cookies("shop").Expires=Date+1 case 2 Response.Cookies("shop").Expires=Date+31 case 3 Response.Cookies("shop").Expires=Date+365 end select end sub 检验是否登陆 以及用户级别 ''************************************************** ''函数名:CheckUserLogined ''作 用:检查用户是否登录 ''参 数:无 ''返回值:True ----已经登录 '' False ---没有登录 ''************************************************** function CheckUserLogined() dim Logined,Password,rsLogin,sqlLogin Logined=True User=Request.Cookies("shop")("User") Password=Request.Cookies("shop")("Password") ''上面是获取cookie if User="" then Logined=False end if if Password="" then Logined=False end if if User="" then Logined=False jibie=9999 end if if Logined=True then user=replace(trim(user),"''","") password=replace(trim(password),"''","") set rsLogin=server.createobject("adodb.recordset") sqlLogin="select * from admin where szd_user =''" & user & "'' and szd_password =''" & password &"''" rsLogin.open sqlLogin,Conn,1,1 if rsLogin.bof and rsLogin.eof then Logined=False else if password<>rsLogin("szd_password") then Logined=False end if User=rsLogin("szd_user") if rsLogin("szd_cookie")=1 then ValidDays=rsLogin("szd_session") elseif rsLogin("szd_cookie")=2 then ValidDays=rsLogin("szd_session")*30 elseif rsLogin("szd_cookie")=3 then ValidDays=rsLogin("szd_session")*365 end if ValidDays=ValidDays-DateDiff("D", rsLogin("szd_last_time"),now()) end if rsLogin.close set rsLogin=nothing end if CheckUserLogined=Logined end function ''************************************************** ''函数名:jibie ''作 用:检查用户级别 ''参 数:LevelNum-----要检查的级别值 ''返回值:级别名称 ''************************************************** function jibieok(jibie) select case jibie case 9999 jibie="游客" case 1 jibie="普通村名" case 2 jibie="勤劳村民" case 3 jibie="高级村民" case 4 jibie="老农" end select end function 显示登陆表单的sub ''================================================== ''过程名:ShowUserLogin ''作 用:显示用户登录表单 ''参 数:无 ''================================================== sub ShowUserLogin() dim strLogin if CheckUserLogined()=False then strLogin="<table align=''center'' width=''100%'' border=''0'' cellspacing=''0'' cellpadding=''0''>" strLogin=strLogin & "<form action=''loginaction.asp'' method=''post'' name=''UserLogin'' onSubmit=''return CheckForm();''>" strLogin =strLogin & "<tr><td height=''25'' align=''right''>用户名:< td height=''25''><input name=''User'' type=''text'' id=''User'' size=''16'' maxlength=''20''></td>" strLogin =strLogin & "<td height=''25'' align=''right''>密码:</td>< td height=''25''><input name=''Password'' type=''password'' id=''Password'' size=''16'' maxlength=''20''></td>" strLogin =strLogin & "<td height=''25'' align=''right''>cookie保存:< /td><td height=''25''><select name=CookieDate><option selected value =0>不 保 存</option><option value=1>保 存 一 天</option>" strLogin =strLogin & "<option value=2>保 存 一 月</option>< option value=3>保 存 一 年</option></select></td>" strLogin =strLogin & "<td height=''30'' colspan=''2''><input name=''Login'' type =''submit'' id=''Login'' value='' 登 录 ''> <input name=''Reset'' type= ''reset'' id=''Reset'' value='' 清 除 ''>" strLogin=strLogin & "<hr size=''1'' color=''#Cccccc''><a href=''User_Reg.asp'' target=''_blank''> 新用户注册</a> <font color=''#666666''>|</font> <a href= ''User_GetPassword.asp''>忘记密码?</a></td>" strLogin=strLogin & "</tr></form></table>" response.write strLogin %> <script language=javascript> function CheckForm() { if(document.UserLogin.User.value=="") { alert("请输入用户名!"); document.UserLogin.User.focus(); return false; } if(document.UserLogin.Password.value == "") { alert("请输入密码!"); document.UserLogin.Password.focus(); return false; } } </script> <% Else response.write " < font color=''#666666''>欢迎</font><font color=#000080>" & Request.Cookies("shop")("User")& "</b></font><font color =''#666666''>,</font>" end if if ValidDays>0 then response.write " < b><font color=blue>" & ValidDays & "<a href=''logout.asp''> 退出</a></font>" if ValidDays<=10 then end if else response.write " < b><font color=red>" & ValidDays & "<a href=''logout.asp''> 退出</a></font>" end if end sub %> 退出时清空cookie <% Response.Cookies("shop")("User")="" Response.Cookies("shop")("Password")="" Response.Cookies("shop")("shop")="" dim ComeUrl ComeUrl=trim(request("ComeUrl")) if ComeUrl="" then ComeUrl=Request.ServerVariables("HTTP_REFERER") if ComeUrl="" then ComeUrl="index.asp" end if end if Response.Redirect ComeUrl %> 以下是原文作者的评论~ 老农修正:保存cookie的时候,原先是session的值,放在数据库里面,取出,写入默认值,这样一般用不到,通用的为: sub SaveCookie_tvillage() Response.Cookies("tvillage")("User")=user Response.Cookies("tvillage")("Password") = PassWord Response.Cookies("tvillage")("jibie") = jibie Response.Cookies("tvillage")("CookieDate") = CookieDate select case CookieDate case 0 ''not save case 1 Response.Cookies("tvillage").Expires=Date+1 case 2 Response.Cookies("tvillage").Expires=Date+31 case 3 Response.Cookies("tvillage").Expires=Date+365 end select end sub
|