
vbscript, asp, and response.redirect - urgent help plz
Hi Anthony!
If you re-organize your ASP page just a bit, it will make it easier to
handle this situation. Here's a general outline I use for many ASP pages:
Dim vars, set default values
Get values from form (if present)
If called from form, then lookup user
If User found Then
Display welcome message, or start page
Else
Display "Bad User or Password" message
Display login form
End If
Else
Display login form
End If
Where it shows "Display login form" above, I usually put that into a sub, so
I don't duplicate the code, or I use flags to indicate what parts are
displayed.
Here's a simple login page:
<%
Dim cn, StrSQL, rsUser
Dim UID, PID
Dim ShowLogin, ErrMsg, UserOK
Set cn = Server.CreateObject("ADODB.Connection")
Set rsUser = Server.CreateObject("ADODB.Recordset")
ErrMsg = ""
ShowLogin = True
UID = ""
PID = ""
UserOK = False
Sub LoginForm()
%><H3>Please Login</H3>
<FORM METHOD="POST" ACTION="login.asp" ID="Login" NAME="Login">
<INPUT TYPE="HIDDEN" NAME="FORMNAME" ID="FORMNAME" VALUE="LOGIN">
User Name: <INPUT TYPE="TEXT" NAME="UID" ID="UID" VALUE=""><BR>
Password: <INPUT TYPE="PASSWORD" NAME="PID" ID="PID" VALUE=""><BR>
<INPUT TYPE="SUBMIT" NAME="SUBMIT" ID="SUBMIT"><BR>
</FORM>
<BR>
<%
End Sub
' Were we called by the login form?
If Request.Form("FORMNAME") = "LOGIN" Then
' Get the values entered
UID = Request.Form("UID")
PID = Request.Form("PID")
' Lookup the user
cn.open (use your conection string stuff here)
StrSQL = "SELECT * FROM Users WHERE User_ID = '" & UID & "' and
PASSWORD = '" & PID & "'"
rsUser.Open StrSQL, cn
If rsUser.State <> 0 Then
If not (rsUser.BOF and rsUser.EOF) Then ' we got a record back
UserOK = True
ShowLogin = False
ErrMsg = "Welcome!"
Else
ErrMsg = "The User ID or Password Entered was Incorrect,
please try again"
End If
End If
End If
If ErrMsg <> "" Then
%><FONT COLOR="FF0000"><% =ErrMsg %></FONT></BR>
<%
End If
If ShowLogin Then
LoginForm
Else
... show your welcome page
End If
%>
Hope that helps...
Sloan
Quote:
> HELP!
> I'm using a database to verify a username and password before letting a
user
> into the main web site.
> The database stuff seems fine - if I enter a valid name and password, it
> prints the appropriate message,
> if I enter an invalid name it says No records found. That's fine.
> However, I'm having a problem with how to send the user to a different
page
> once the LOGIN.ASP deals with it.
> I have included the output from the webpage, the HTML and the ASP.
> Any help is appreciated, and I'm supposed to have this ready in about 8
> hours...
> ============================================
> OUTPUT
> =======
> No records found
> Response object error 'ASP 0156 : 80004005'
> Header Error
> /test/login.asp, line 42
> The HTTP headers are already written to the client browser. Any HTTP
header
> modifications must be made before writing page content.
> ============================================
> HTML
> =====
> <HTML>
> <HEAD>
> <TITLE>Login Page</TITLE>
> </HEAD>
> <BODY>
> <FORM NAME="form" METHOD="POST" ACTION="login.asp">
> Username: <INPUT TYPE="text" NAME="username"><BR>
> Password: <INPUT TYPE="text" NAME="password"><BR>
> <INPUT TYPE="SUBMIT" NAME="LOGIN">
> </FORM>
> </BODY>
> </HTML>
> ASP
> ===
> <HTML>
> <HEAD>
> <TITLE>LOGIN AUTHORISATION PAGE</TITLE>
> </HEAD>
> <BODY BGCOLOR="WHITE">
> <BR><BR><BR>
> <CENTER>
> <%
> 'Request.form function request the value from the form.
> iUsername = server.HtmlEncode(request.form("username"))
> iPassword = server.HtmlEncode(request.form("password"))
> iRedirect = server.HtmlEncode(request.form("redirect"))
> 'Create object. In this case Connection to a database
> Set Conn = Server.CreateObject("ADODB.Connection")
> 'Select provider
> Conn.Provider = "Microsoft.Jet.OLEDB.4.0"
> 'Select data source.
> Conn.ConnectionString = "Data Source=" & Server.MapPath ("login.mdb")
> 'Open the connection
> Conn.Open
> 'Create recordset
> Set Rs = Server.CreateObject("ADODB.Recordset")
> 'Open recordset with the connection which we have created earlier
> Rs.Open "SELECT * from Table1;", Conn, 1,3
> 'response.write Build & "<BR>"
> rs.Filter = "username = '"&iUserName&"'"
> If rs.BOF And rs.EOF Then
> Response.Write("<BR><BR><BR><CENTER><b>No records found</b></CENTER>")
> Response.Redirect("login.html")
> End If
> if rs("password") <> iPassword then
> response.redirect("login.html")
> end if
> 'if we get here, then we're logged in properly
> Response.Redirect("index.html")
> 'Deinitialize the Connection and Recordset
> set Rs = nothing
> set Conn = nothing
> %>
> </CENTER>
> </BODY>
> </HTML>