מדריך ASP – עבודה מול DB – חלק ג'

Login

בשלב הראשון המשתמש מגיע לדף הכניסה enter.asp, ומתבקש להכניס שם וסיסמא. אם הוא עדיין אינו רשום במערכת,
הוא ילחץ על לינק המפנה לדף subscribe.asp, שם יבצע את ההרשמה. אם יש ברשותו סיסמא, הוא יקליד אותה,
הסיסמא ושם המשתמש ייבדקו מול בסיס הנתונים, ואם הם קיימים, הכניסה תאושר.

הטבלה נראית כך:





הקובץ enter.asp

VBScriptJScript

<%
If Request("Login")<>"" And Request("Password")<>"" Then
   Dim conn, rs, SQLstr, okForProccess
   
   Set conn = Server.CreateObject("ADODB.Connection") 'connection object
   Set rs = Server.CreateObject("ADODB.Recordset") 'recordset object
   conn.Open "myDSN"    'oppening the connection

   SQLstr="SELECT ID,Password FROM SiteUsers "
   SQLstr= SQLstr & "WHERE Login='" & Request("Login") & "'"    ' SQL query
   rs.Open SQLstr,conn    'oppening the recordset

   
   If Rs.EOF Then 'if there are no corresponding records
     okForProccess=false 'Invalid login
   Else
     If Request("Password")<>rs("Password") Then
           okForProccess=false 'Invalid password
     Else
          okForProccess=true 'valid password
         Session("ID") = rs("ID")
     End If
   End If

   rs.Close
   conn.Close
   Set rs = Nothing
   Set conn = Nothing

   If okForProccess Then ' both login and password are valid
        Response.Redirect "securedPage.asp"
   Else
      Response.Write "<center><FONT COLOR='red'>"    'displaying error message
      Response.Write "Invalid login or password</FONT></center>"
   End If
End If
%>

<html>
<head>
   <title>enter.asp</title>
</head>
<body>
<form method=post action="enter.asp">
<table width="50%" align="center">
<tr>
    <td>login:</td>
    <td><input type="text" name="login"></td>
</tr>
<tr>
    <td>password:</td>
    <td><input type="text" name="password"></td>
</tr>
<tr>
    <td colspan=2><input type="submit"></td>
</tr>
</table>
</form>
<table width="50%" align="center">
	<tr>
		<td><a href="subscribe.asp">
			if you are still not registred, press here</a></td>
	</tr>
</table>
</body>
</html>


<%@ Language=JScript%>
<%
if((Request("Login")() && Request("Password")()){
   var conn = Server.CreateObject("ADODB.Connection") //connection object
     , rs = Server.CreateObject("ADODB.Recordset") //recordset object
     , SQLstr, okForProccess
   
   conn.Open ("myDSN" ) //oppening the connection

   SQLstr="SELECT ID,Password FROM SiteUsers "
   SQLstr+= "WHERE Login='" + Request("Login")() + "'" // SQL query
   rs.Open (SQLstr,conn) //oppening the recordset

   
   if(rs.EOF ){ //if(there are no corresponding records
     okForProccess=false //Invalid login
   }else{
     if(Request("Password")()!=rs("Password").value){
           okForProccess=false //Invalid password
     }else{
         okForProccess=true //valid password
         Session("ID") = rs("ID").value
     }
   }

    rs.Close // closing objects
    delete rs;
    rs = null;
    conn.Close
    delete conn;
    conn = null;

   if(okForProccess ){ // both login and password are valid
        Response.Redirect "securedPage.asp"
   }else{
      Response.Write ("<center><FONT COLOR='red'>") //displaying error message
      Response.Write ("Invalid login or password</FONT></center>")
   }
}
%>

<html>
<head> <title>enter.asp</title></head>
<body>
<form method=post action="enter.asp">
<table width="50%" align="center">
<tr>
    <td>login:</td>
    <td><input type="text" name="login"></td>
</tr>
<tr>
    <td>password:</td>
    <td><input type="text" name="password"></td>
</tr>
<tr>
    <td colspan=2><input type="submit"></td>
</tr>
</table>
</form>
	<a href="subscribe.asp">
			if you are still not registred, press here
	</a>
</body>
</html>

במקרה ומצאנו כי שם המשתמש והסיסמא קיימים במערכת, אנו מכניסים את מספר ה-ID של המשתמש למשתנה Session, כדי לוודא בדפים הבאים שלמשתמש אכן יש אישור לצפות בהם. כך נמנע מצב שמשתמש שמקליד בשורת הכתובת של הדפדפן את כתובתו של אחד הדפים אשר איננו רוצים שמשתמשים לא-רשומים יראו, יראה בכל זאת את הדף.
בראשו של כל דף "מאובטח" נכתוב:

VBScriptJScript

<%
    If Session("ID")="" Then
        Response.Redirect "enter.asp"
    End If
%>


<%
    if (!Session("ID")){
        Response.Redirect ("enter.asp")
        Response.End()
    }
%> 

כך נוודא שכל משתמש המנסה לראות דף מאובטח הגיע אלינו באמצעות הכנסת שם וסיסמא.אם לא – אנו
שולחים אותו לדף המבקש שם וסיסמא. (יש לציין שאותו דבר ניתן לעשות גם באמצעות עוגיות).
הקובץ subscribe.asp

VBScriptJScript

<%
   If Request("Login")<>"" And Request("Password")<>"" Then
      'only login & password are required
      Dim conn, SQLstr
      Set conn = Server.CreateObject("ADODB.Connection")
      conn.Open "myDSN"

      SQLstr="INSERT INTO SiteUsers (Login,Password,Fname,Lname,Email)"
      SQLstr= SQLstr & " VALUES ('" & Request("Login") & "','"
      SQLstr= SQLstr & Request("Password") & "','"
      SQLstr= SQLstr & Request("Fname") & "','"
      SQLstr= SQLstr & Request("Lname") & "','"
      SQLstr= SQLstr & Request("Email") & "')"
      conn.Execute SQLstr
      conn.Close
      Set conn = Nothing
      Response.Redirect "enter.asp"
   End If
%>
<html>
<head>
   <title>subscribe.asp</title>
</head>
<body>
<form method=post action="subscribe.asp">
<table width="50%" align="center">
<tr>
    <td>login:</td>
    <td><input type="text" name="login"></td>
</tr>
<tr>
    <td>password:</td>
    <td><input type="text" name="password"></td>
</tr>
<tr>
    <td>first name:</td>
    <td><input type="text" name="fname"></td>
</tr>
<tr>
    <td>last name:</td>
    <td><input type="text" name="lname"></td>
</tr>
<tr>
    <td>email:</td>
    <td><input type="text" name="email"></td>
</tr>
<tr>
    <td colspan=2><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html>


<%@ Language=JScript%>
<%
   if (Request("Login" ++ Request("Password")){
      //only login + password are required
      var conn = Server.CreateObject("ADODB.Connection")
        , SQLstr
      conn.Open ("myDSN")

      SQLstr="INSERT INTO SiteUsers (Login,Password,Fname,Lname,Email)"
      SQLstr+= " VALUES ('" + Request("Login")() + "','"
      SQLstr+= Request("Password")() + "','"
      SQLstr+= Request("Fname")() + "','"
      SQLstr+= Request("Lname")() + "','"
      SQLstr+= Request("Email")() + "')"
      conn.Execute (SQLstr)
     conn.Close
     delete conn;
     conn = null;
      Response.Redirect ("enter.asp")
   }
%>
<html>
<head>
   <title>subscribe.asp</title>
</head>
<body>

<form method=post action="subscribe.asp">
<table width="50%" align="center">
<tr>
    <td>login:</td>
    <td><input type="text" name="login"></td>
</tr>
<tr>
    <td>password:</td>
    <td><input type="text" name="password"></td>
</tr>
<tr>
    <td>first name:</td>
    <td><input type="text" name="fname"></td>
</tr>
<tr>
    <td>last name:</td>
    <td><input type="text" name="lname"></td>
</tr>
<tr>
    <td>email:</td>
    <td><input type="text" name="email"></td>
</tr>
<tr>
    <td colspan=2><input type="submit"></td>
</tr>
</table>
</form>
</body>
</html> 

לאחר שהמשתמש נרשם, אנו שומרים את פרטיו בבסיס הנתונים, ומעבירים אותו, באמצעות Response.Redirect לדף המאובטח. 
בפעם הבאה שהוא יגיע לאתר, הוא כבר לא יצטרך להרשם שוב, אלא רק להכניס את שם המשתמש והסיסמא שלו.

תגיות: , , , , ,

תגובות בפייסבוק