热门文章
 
ASP内建对象Server
Global.asa文件的使
ASP内建对象Applica
访问和更新Cookies集合
Asp:Cookie集合 简
asp中cookie使用示例
Asp:Cookie集合简介
遍历cookies的方法
判断cookie是否开启
cookies读写基本例子
 推荐文章
 
ASP Session 对象
松子谈asp的Request
再总结一下asp重定向的方法
asp:Session St
session的使用与性能:
asp session 使用
asp中session具体是
防止ASP Session丢
Asp提高首页性能的一个技巧
ASP禁止刷新当前页
asp清空session 关
TypeLibrary 声明
cookies和sessio
web开发:Session详
ASP中Session技巧
再谈asp中的Server.
 
你现在的位置:您现在的位置是: 中国ASP>>ASP教程>>ASP对象
asp中cookie使用示例

很久以前我和某位人士的讨论。

首先是前台的表单域:

<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_5do8()
sub SaveCookie_5do8()
Response.Cookies("5do8")("User")=user
Response.Cookies("5do8")("Password") = PassWord
Response.Cookies("5do8")("CookieDate") = CookieDate
select case CookieDate
case 0
''''not save
case 1
Response.Cookies("5do8").Expires=Date+1
case 2
Response.Cookies("5do8").Expires=Date+31
case 3
Response.Cookies("5do8").Expires=Date+365
end select
end sub

检验是否登陆 以及用户级别

''''**************************************************
''''函数名:CheckUserLogined
''''作  用:检查用户是否登录
''''参  数:无
''''返回值:True ----已经登录
''''        False ---没有登录
''''**************************************************
function CheckUserLogined()
dim Logined,Password,rsLogin,sqlLogin
Logined=True
User=Request.Cookies("5do8")("User")
Password=Request.Cookies("5do8")("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("5do8")("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("5do8")("User")=""
Response.Cookies("5do8")("Password")=""
Response.Cookies("5do8")("5do8")=""

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

相关信息:

Cookie,Session,Application封装
访问和更新Cookies集合
Asp:Cookie集合 简介
asp中cookie使用示例
Asp:Cookie集合简介
遍历cookies的方法
判断cookie是否开启
cookies读写基本例子

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008