ASP+ACCESS用戶登錄驗證代碼

用戶驗證一:

admin文件夾下有login.asp,conn.asp,check.asp,index.asp,data文件夾下是data.mdb,數據庫中有數據表admin,表結構是id,自動編號,username,文本,password,文本
       下面是各文件的代碼:
       index.asp
       <%
         if not session("check")="checked" then
             response.Redirect "login.asp"
         else
       %>
      <html>
        <head>
           <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
           <title>後臺管理</title>
        </head>
        <frameset cols="100,*" border="1" framespacing="1" rows="*">
        <frame name="left" scrolling="auto" src="left.asp" />
        <frame name="right" scrolling="auto" src="right.asp"  />
        </frameset><noframes></noframes>
        <noframes>
      </html>
    <%
      end if
     %>

     conn.asp
     <%
       Set conn=server.CreateObject("adodb.connection")
       connstr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath("../data/data.mdb")
       conn.open connstr
     %>

    login.asp
    <html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <title>後臺登陸驗證</title>
     </head>
     <body>
       <form  name="form1" method="post" action="check.asp">
           <div align="center">
           <p>&nbsp;</p>
           <p>後臺管理系統入口</p>
           </div>
          <div align="center"><br />
           帳號:
          <input type="text" name="username" />
           <br />
           <br />
          密碼:
          <label>
           <input type="password" name="password" />
          </label>
          </div>
          <p align="center">
           <label>
            <input type="submit" name="Submit1" value="提交" />
           </label>
           <label>
           <input type="reset" name="Submit2" value="重置" />
           </label>
           </p>
        </form>
        </body>
       </html>

      check.asp
      <!--#include file="conn.asp"-->
      <title>嗨,出錯了!</title>
       <%
         username=replace(request.form("username"),"'","''")
         password=replace(request.form("password"),"'","''")
         sql="select * from admin where(username='"&username&"' and password='"&password&"')"
         set rs=server.createobject("adodb.recordset")
         rs.open sql,conn,1,1
         if not rs.eof then
         rs.close
         conn.close
         session("check")="checked"
         response.Redirect "index.asp"
        else
        session("check")=""
        response.write ("對不起,密碼錯誤,請重新覈對後再登陸!")
        end if
        %>

 

用戶驗證二:


先來看 login.htm 頁面 

     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
     <title>登錄</title> 
     </head>
     <body> 
        <form name="form1" method="post" action="logincheck.asp">
          <table width="200" border="1" align="center" cellpadding="0" cellspacing="0"> 
          <tr> 
          <td colspan="2"><div align="center">登錄</div></td>
          </tr>
          <tr> 
          <td width="57"><div align="center">用戶名</div></td>
          <td width="137" valign="top"><input name="username" type="text"  id="username" size="25"></td> 
          </tr>
          <tr> 
          <td><div align="center">密碼</div></td> 
          <td valign="top"><input name="password" type="password"  id="password" size="25"></td>
          </tr>
          <tr> 
          <td colspan="1"><div align="center"> 
          <input type="submit" name="Submit" value="提交">
          </div></td>
          </tr>
          </table>
        </form>
      </body>
      </html>

    後臺驗證頁面 logincheck.asp

     <%
      dim username,password 
      username=request.form("username")
      password=request.form("password")
      set rs=server.createobject("adodb.recordset") 
      conn = "DBQ=" + server.mappath("user.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};" 
      sql="select * from user where y_user='"&username&"'"
      rs.open sql,conn,1,1
      if rs.eof then 
      response.write "<script>alert('登錄失敗');history.back();</script>"
      response.end
      else 
      if rs("y_pass")<>password then
      response.write "<script>alert('登錄失敗');history.back();</script>"
      response.end     
      end if      
      session("admin")=username
      response.write"<script>alert('登錄成功');location.href='index.asp'</script>"
      end if 
     %>

    數據庫用access,數據庫名:user.mdb
         數據表名:user
         用戶名字段:y_user,密碼字段:y_pass.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章