Active Server Pages Programming (ASP)/authentication
Expert: Srini Nagarajan - 11/29/2004
Questionhi
i got this code and i want to use it in my system for check the username, password and domain or the user. but when i run the code, there is some error occur at line :
Set rs = con.Execute(sq)
it tell expected "con"
here is the full code that i want to use in every page of my system. i'm using HTML and ASP using VB script and my database is in MS ACCESS.
the code :
<%
' this file is designed to be #included in other pages
' it checks for login authentication
' if the user is authenticated it does nothing
' if the user is not authenticated it redirects to the login page
'
' this version uses session variable "member" for authentication
' if you wanted to use some other authentication method, just change this #include file
If Instr(LCase(Request.ServerVariables("script_name")), "login.asp") = 0 Then
If Session("member") = "" Then
' user is not logged in... redirect to the login page
Response.Redirect "login.asp"
End If
End If
' otherwise continue with the current page
' function Check_Authorization takes a username and password and domain
' and returns true if they are valid, or false if not...
' as a side-effect it sets several session variables
' Session("member") string, username
' Session("is_admin"), boolean, if user is admin
' Session("is_staff"), boolean, if user is staff
' Session("is_student"), boolean, if user is staff
' assume a global variable "con" points to an open connection
Function Check_Authorization(uname, upass, domain)
Dim dataConn
Dim sq
Dim rs
On Error Resume Next
Set dataConn = Server.CreateObject("ADODB.Connection")
dataConn.Open "Driver={Microsoft Access Driver (*.mdb)};"&_
"DBQ=" & Server.MapPath("dbase.mdb")
If Err.number = 0 Then
On Error Goto 0
sq = "select * from login where username='" & _
Replace(uname, "'", "''") & "' and userpass='" & _
Replace(upass, "'", "''") & "' and domain='" & _
Replace(domain, "'", "''") & "'"
Set rs = con.Execute(sq)
If rs.EOF Then
Check_Authorization = False
Session("member") = ""
Session("is_admin") = False
Session("is_staff") = False
Session("is_student") = False
ElseIf IsNull(rs(0)) Then
Check_Authorization = False
Session("member") = ""
Session("is_admin") = False
Session("is_staff") = False
Session("is_student") = False
Else
Check_Authorization = True
Session("member") = rs(0)
Session("is_admin") = cBool(Lcase(domain) = "administrator")
Session("is_staff") = cBool(Lcase(domain) = "staff")
Session("is_student") = cBool(Lcase(domain) = "student")
End If
rs.Close
Set rs = Nothing
dataConn.Close
Set dataConn = Nothing
Else
Response.Write "Unable to open database. " & Err.Description
Check_Authorization = False
End If
End Function
%>
sorry bcoz i paste it here.. if u have an email, i can attach directly to you..
really need ur help!
tq.
AnswerHi
Set dataConn = Server.CreateObject("ADODB.Connection")
dataConn.Open "Driver={Microsoft Access Driver (*.mdb)};"&_
"DBQ=" & Server.MapPath("dbase.mdb")
you are using "dataConn" and you can't execute "con" change it to "dataConn.Execute(sq)"
Happy Programming!
-Sri