菜鸟十天学会ASP教程之第九天:Session组件

80酷酷网    80kuku.com

  session|教程|十天学会

  上一节:

  学习目的:基本的SESSION组件,总结response,request组件。

  首先,有会员系统的任何程序都会用到检测是不是用户已经登陆这个步骤。这就用到了SESSION组件,下面我们 看一个代码来说明。

<%
session("login")="yes"
%>

  这句话的意思就是在session里面定义一个login字符串变量,值为"yes",直接可以赋值,不需要声明。是不是很简单?

  如果我们做管理员登陆系统的话,首先是一段检测是不是管理员:

if 是 then
session("isadmin")=yes"
else
session("isadmin")="no"
end if

  在每一个需要管理员才能看的页面最前面加上:

<%
if not session("isaadmin")="yes" then
response.redirect "login.htm"
%>

  这样一般用户就无法打开这个页面。解释一下response.redirect,它是转向的意思,后面的"login.htm"就是转向的文件。这样没有登陆的管理员是无法看到后面的内容的。

  response组件基本就是用到response.write(),response.redirect() 分别是写字符串和转向的作用。

  request基本就是request.form(),request.querystring() 分别是接受post,get方法传来的信息。

  最后我们一起来制作一个简单的后台登陆管理界面,首先在myweb目录下建立一个admin文件夹,然后我们建立一个数据库名字为admin.mdb,然后我们再建立一个表,表中设置两个字段name,password,类型都是文本型的!最后退出时设置主键,保存为表名check。然后可以输入一条记录用户名:admin,密码:admin。具体建立数据库的方法请看《》

  下面我们开始编写ASP程序,首先建立一个index.asp(管理主界面)程序,代码如下:

<%language=vbscript%>
<%if not session("checked")="yes" then
response.Redirect "login.asp" 
else
%>
<html>
<head>
<title>管理界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset cols="167,*" frameborder="YES" border="1" framespacing="1" rows="*" bordercolor="#666666">
  <frame name="leftFrame" scrolling="auto" noresize src="left.asp">
  <frame name="mainFrame" src="right.asp">
</frameset>
<noframes>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</noframes>
</html>
<%end if%>

  在上面的代码中,大家可以看到用到login.asp,left.asp,right.asp程序

  login.asp://登陆系统程序

<html>
<head>
<title>管理员入口</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.topic {  font-family: "宋体"; font-size: 11pt; font-weight: bold; color: #FFFFFF}
.font {  font-family: "宋体"; font-size: 10pt; font-weight: normal; color: #000000}
.table {  border-color: #666666 black; border-style: solid; border-top-width: 1pt; border-right-width: 0px; border-bottom-width: 1pt; border-left-width: 0px}
.text {  border: 1pt #999999 solid; height: 15pt}
-->
</style>
</head>

<body text="#000000" topmargin="0" bgcolor="#FFFFFF">
<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" height="100%">
  <tr>
    <td height="129" valign="top" colspan="3">  </td>
  </tr>
  <tr>
    <td width="230" height="170" valign="top"> </td>
    <td valign="top" width="277">
      <table width="100%" border="0" cellspacing="1" cellpadding="0" height="100%" bgcolor="#000000" align="center">
        <tr>
          <td align="center" valign="middle" height="167">
            <form name="form1" method="post" action="check.asp">
              <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
              <tr bgcolor="#62892C">
                <td height="31" class="topic" colspan="2">
                  <div align="center">管理员入口

                  </div>
                </td>
              </tr>
              <tr>
               
                  <td bgcolor="#87bc3c" colspan="2" class="table">
                    <div align="center"> <span class="font"> 管理员:</span>
                      <input type="text" name="name"  class="text" size="20" >
                     

                      <span class="font">密 码:</span>
                      <input type="password" name="password" class="text" size="20" ><%if session("check")="wrong" then response.Write "
<span class='font'><font color=red>验证错误!</font></span>" end if%>
                    </div>
                  </td>
            
              </tr>
              <tr>
                <td bgcolor="#87bc3c" width="52%">
                    <div align="center" class="font">
                      <input type="reset" name="Submit2" value="重置" class="text">
                    </div>
                </td>
                <td bgcolor="#87bc3c" width="48%">
                    <div align="center" class="font">
                      <input type="submit" name="Submit22" value="提交" class="text">
                    </div>
                </td>
              </tr>
            </table>   </form>
          </td>
        </tr>
      </table>
    </td>
    <td width="241" valign="top"> </td>
  </tr>
  <tr>
    <td height="123" valign="top" colspan="3"> </td>
  </tr>
</table>
</body>
</html>

  在上面的程序中用到一个检查用户和密码是否正确的程序check.asp://核对输入的用户和密码是否正确

<%
dim name,password
name=request.form("name")
password=request.form("password")
dim exec,conn,rs
exec="select *from check where(name='"&name&"' and password='"&password&"')"
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("admin.mdb")
set rs=server.createobject("adodb.recordset")
rs.open exec,conn
if not rs.eof then
rs.Close
conn.Close
session("checked")="yes"
session("check")="right"
response.Redirect "index.asp"
else
session("checked")="no"
session("check")="wrong"
response.Redirect "login.asp"
end if
%>

  left.asp://管理导航

<>
<%if not session("checked")="yes" then
response.Redirect "login.asp" 
else
%>
<html>
<head>
<title>管理界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body text="#000000" topmargin="0" bgcolor="#ffffff" leftmargin="10">
<div align="center"><a href="index.asp" target="_parent">

 

  管理界面首页</a> <a href="exit.asp" target="_parent">退出</a>
 
 

</div>
</body>
</html>
<%end if%>

  exit.asp://退出系统

<%language=vbscript%>
<%
session("check")=""
session("checked")=""
response.redirect "login.asp"
%>

  right.asp://具体管理的内容

<%language=vbscript%>
<%if not session("checked")="yes" then
response.Redirect "login.asp" 
else
%>
<html>
<title>管理界面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#ffffff" text="#000000" topmargin="20" class="title">

这里是网页教学网的管理系统示例!请大家多研究使用!
</body>
</html>
<%end if%>

  运行时首先运行index.asp程序,运行效果部分截图如下:

  下一节:

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: