HTTP 是不纪录连接状态的协议,这给服务器减少了许多负担。至少,不需要为每一个连接的用户开辟一个缓冲区了。但是,很多时候用户想要的体验是自己在访问一个连贯的页面。譬如说,有些页面在登陆之前不能访问,但登陆成功一次之后应该在一段时间内不用重复登陆。又比如说,用户想把自己感兴趣的货品先放在购物车里,最后在一起付款。
这些时候Cookies 和 Session 都可以很方便的满足这些需求,让用户感到网站是在围绕着自己服务的。
Cookie 和 Session 是一对有所区别但又常在一起使用的技术。区别在于, Cookie 在客户端纪录用户信息,而Session 则工作在服务器端。
Cookie 的中文名是储存在本地终端上的数据,可以通过点击浏览器地址栏旁边的 帮助符号查看当前连接中的 Cookie。 比如,我在写这篇博客时的Cookie 有39个
可以看到Cookie 的分类是基于域名的, 所以跨域名之间的Cookie 不会共享,这保证了安全性。 而同一个域名下跨页面的Cookies 可以共享, 这又给使用带来了方便。
举个例子, 记住密码在登陆时是很常用的选项,在前面的登陆页面中也预留了记住密码的复选框。 用Cookie 可以很简单的实现记住密码的功能。 在服务器端判断用户有没有勾选记住密码的复选框,如果勾选的话,就在Cookie 里加上 “PWD ”属性,然后把它的值设为 用户密码。而如果用户没有勾选的话,我们就要即时的清除这个存储在 Cookie 里面存储的密码。
而在用户访问登陆页面时,只需要先判断Cookie 里是不是存有密码就可以了。
客户端的登陆页面 (loginpage.asp)如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>登陆界面</title>
<style>
html {
background-color: #B5DEF2;
}
.wrapper {
margin-left: 35%;
margin-top: 100px;
width: 400px;
}
a:link{
color: #B7D4EA;
}
a:visited{
color: darkcyan;
}
a:hover{
color: black;
}
.loginBox {
background-color: #F0F4F6;
/*上divcolor*/
border: 1px solid #BfD6E1;
border-radius: 5px;
color: #444;
font: 14px 'Microsoft YaHei', '微软雅黑';
margin: 0 auto;
width: auto;
}
.loginBox .loginBoxCenter {
border-bottom: 1px solid #DDE0E8;
padding: 24px;
}
.loginBox .loginBoxCenter p {
margin-bottom: 10px
}
.loginBox .loginBoxButtons {
/*background-color: #F0F4F6;*/
/*下divcolor*/
color: darkcyan;
border-top: 0px solid #FFF;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
line-height: 28px;
overflow: hidden;
padding: 20px 24px;
vertical-align: center;
filter: alpha(Opacity=80);
-moz-opacity: 0.5;
opacity: 0.5;
}
.loginBox .loginInput {
border: 1px solid #D2D9dC;
border-radius: 2px;
color: #444;
font: 12px 'Microsoft YaHei', '微软雅黑';
padding: 8px 14px;
margin-bottom: 8px;
width: 300px;
}
.loginBox .loginInput:FOCUS {
border: 1px solid #B7D4EA;
box-shadow: 0 0 8px #B7D4EA;
}
.loginBox .loginBtn {
background-image: -moz-linear-gradient(to bottom, blue, #85CFEE);
border: 1px solid #98CCE7;
border-radius: 20px;
box-shadow: inset rgba(255, 255, 255, 0.6) 0 1px 1px, rgba(0, 0, 0, 0.1) 0 1px 1px;
color: #444;
/*登录*/
cursor: pointer;
float: right;
font: bold 13px Arial;
padding: 10px 50px;
}
.loginBox .loginBtn:HOVER {
background-image: -moz-linear-gradient(to top, blue, #85CFEE);
}
.loginBox a.forgetLink {
color: #ABABAB;
cursor: pointer;
float: right;
font: 11px/20px Arial;
text-decoration: none;
vertical-align: middle;
/*忘记密码*/
}
.loginBox a.forgetLink:HOVER {
color: #000000;
text-decoration: none;
/*忘记密码*/
}
.loginBox input#remember {
vertical-align: middle;
}
.loginBox label[for="remember"] {
font: 11px Arial;
}
</style>
</head>
<body>
<%
Session("login")=false
dim StrUID
dim StrPWD
StrUID=Request.Cookies("User")("UID")
StrPWD=Request.Cookies("User")("PWD")
%>
<div class="wrapper">
<form action="login.asp" method="post">
<div class="loginBox">
<div class="loginBoxCenter">
<p><label for="username">用户名:</label></p>
<p><input type="text" id="username" name="username" class="loginInput" autofocus="autofocus" required="required" autocomplete="off" placeholder="请输入邮箱/手机号" value="<%=StrUID %>" /></p>
<p><label for="password">密码:</label></p>
<p><input type="password" id="password" name="password" class="loginInput" required="required" placeholder="请输入密码" value="<%=StrPWD %>" /></p>
<p><a class="forgetLink" href="#">忘记密码?</a></p>
<input id="remember" type="checkbox" name="remember" />
<label for="remember">记住登录状态</label>
</div>
<div class="loginBoxButtons">
<button class="loginBtn">登录</button>
<div> <a href="agent.asp">用户注册</a> </div>
</div>
</div>
</form>
</div>
</body>
</html>
服务器端的登陆页面代码(login.asp)如下:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
set rs=Server.CreateObject("ADODB.Recordset")
conn.Open "ASP"
dim UID
dim PWD
UID=Request.Form("username")
PWD=Request.Form("password")
if conn.state=1 then
rs.open "select * from UID_PWD where UID='"&UID&"' ",conn
if(rs("PWD")=PWD) then
conn.close
Session("login")&