CSDN博客

img xuandhu

cookies读写删

发表于2004/9/16 13:18:00  1034人阅读

分类: 其它

<%@ language="VB"%>
<%@ import namespace="System" %>
<%@ import namespace="System.Web" %>
<%@ import namespace="System.Web.HttpCookie" %>
<HTML>
<title>Cookies的写入和读取</title>
 <script language="VB" runat="server">

  Sub btn1(sender as object,e as eventargs)
'判断是否浏览者选取了允许保存Cookies的选项和输入的账号和密码是否为空
   if Checkbox1.checked and Not(name.text ="") and Not(ps.text ="") then
'向Cookies中写入浏览者输入的账号和密码
   dim cookie as HttpCookie
   cookie=new HttpCookie("mychoices")
          cookie.Values.Add("name",name.text)
          cookie.Values.Add("ps",ps.text)
   Dim dt As DateTime = DateTime.Now
'加入当前时间
   cookie.Values.Add("time",dt.tostring)
   Dim ds As timespan
   ds = new timespan(0,0,10,2)
'保存入Cookies   
   cookie.Expires=dt.add(ds)
   response.appendcookie(cookie)
   
   label1.text="<font color=red size=5>成功向Cookies写入</font>"
  
 Else
  label1.text="<font color=red size=5>没有存入任何内容到Cookies中</font>"
 End If
'为了保证安全性,清空用户的输入
  name.text=""
 ps.text=""
 End Sub

 Sub btn2(sender as object,e as eventargs)
'判断是否存在该Cookies
 If request.cookies("mychoices") Is Nothing then
  label1.text="<font color=red size=5>没有这个Cookies</font>"
 Else
  
  dim cookie as HttpCookie
  cookie=new HttpCookie("mychoices")
'如果以前保存的用户名和账号不为空则读出账号和密码
   If Not(request.cookies("mychoices").values("name") ="") And Not(request.cookies("mychoices").values("ps") = "")
  label3.text ="保存在cookies中的用户名:" & "<font color=red size=5>" & request.cookies("mychoices").values("name") & "</font>"
  label4.text ="保存在cookies中的密 码 :" & "<font color=red size=5>" & request.cookies("mychoices").values("ps") & "</font>"
  label1.text="<font color=red size=5>成功读出Cookies中内容</font>"

   Else
'否则显示错误信息
   label1.text="<font color=red size=5>Cookies中没有任何内容</font>"
   End If
 End If

 End Sub

 Sub btn3(sender as object,e as eventargs)
'判断是否存在该Cookies
 If request.cookies("mychoices") Is Nothing then
  label1.text="<font color=red size=5>没有这个Cookies</font>"
 Else
'如果以前保存的用户名和账号不为空,则清除账号和密码
  If Not(request.cookies("mychoices").values("name") ="") And Not(request.cookies("mychoices").values("ps") = "")
  dim cookie as HttpCookie
  cookie=new HttpCookie("mychoices")
         cookie.Values.Add("name","")
         cookie.Values.Add("ps","")
  label3.text =""
  label4.text =""
  Dim dt As DateTime = DateTime.Now
'加入当前时间
   cookie.Values.Add("time",dt.tostring)
   Dim ds As timespan
   ds = new timespan(0,0,10,2)
'保存入Cookies   
   cookie.Expires=dt.add(ds)
  response.appendcookie(cookie)
  label1.text="<font color=red size=5>成功清除Cookies中内容</font>"
  Else
'否则显示错误信息
  label1.text="<font color=red size=5>Cookies中没有任何内容可供清除</font>"
  End If
 End If

 End Sub


 </script>
 <body>
    <h3><font face="Verdana">Cookies的写入和读取</font></h3><br><Br>
  <Form runat="server">
   <P>
    输入您的用户名:<asp:TextBox id="name" Columns="25" MaxLength="30" runat="server" />
    <br>
    输入您的密 码 :<asp:TextBox id="ps" Columns="25" MaxLength="30" runat="server" TextMode="Password" />
    <asp:button id="button1" OnClick="btn1" text=" 登录" runat="server" />
   </P>
   <P>
    &nbsp;
    <asp:CheckBox id="CheckBox1" runat="server" Checked="True"></asp:CheckBox>
    <asp:Label id="Label2" runat="server">保存账号密码到Cookies</asp:Label>
   </P>
   <P>
    <FONT face="宋体">&nbsp;</FONT>
    <asp:label id="label3" text="" runat="server" />
    <BR>
    <FONT face="宋体">&nbsp;</FONT>
    <asp:label id="label4" text="" runat="server" />
   </P>
   <p>
    <asp:button id="button2" OnClick="btn2" text="读出cookies" runat="server" />
    <asp:button id="button3" OnClick="btn3" text="清除cookies" runat="server" />
    <BR>
   <p>
    <asp:label id="label1" text="" runat="server" />
  </Form>
  </P>
 </body>
</HTML>

0 0

相关博文

我的热门文章

img
取 消
img