Print Page | Close Window

redirect to 2 diff pages based on If else

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=13933
Printed Date: 30 March 2026 at 1:25pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: redirect to 2 diff pages based on If else
Posted By: elbeardo
Subject: redirect to 2 diff pages based on If else
Date Posted: 21 February 2005 at 8:21pm
hi ive searched all the forums etc and havent found an answer.

I'm using the asp login code and edited it to my own needs as im a propler newbie!
However, the example doesnt give an example of when you have in your database a "user type" field. This is for the type of user that logs in i.e admin or a normal user.
What i want it to do is if the user loging in is admin ,...they are redirected to one page and if the user is a normal user they are directed to another.
 I've tried this sql
"SELECT user.password, user.type FROM
user WHERE user.username ='" & strusername & "'"


basically all thats different here is that the "type" column is selected too. I then used this with:
'redirect
        IF rsCheckuser("type") = "admin" THEN
        Response.Redirect"successfulLogin.asp?name=" & strusername
        ELSE
        response.Redirect("staffframe.asp")
    END IF
    End IF
   
End If 

i know its either a really simple modification or ive got it completely wrong but ive spent sooo long on this i would be so grateful for any help.
ps - basically all ive changed are a few variables to fit in with my database, and the basic login and sessions worked fine with those.

btw if anyone want to help me out im ok if you use the original example coding and db.
rob


-------------
dude



Replies:
Posted By: ljamal
Date Posted: 21 February 2005 at 9:12pm

if LCase(rsCheckUser("type")) = "admin" then
Response.Redirect "successfulLogin.asp?name=" & strusername
else
Response.Redirect("staffframe.asp")
end if


-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: elbeardo
Date Posted: 22 February 2005 at 7:48am
Thanks for the quick reply!
I'm not sure where the "LCase" comes from on that first line you gave me but i tried it anyway and the error its giving me is "Type mismatch" on that line. I think this was the same error i was getting before.
Also, is the way i queried the database to get the "type" record a valid way of doing this, or should it be queried separately?

thanks again so much for your help.
rob


-------------
dude


Posted By: Gullanian
Date Posted: 22 February 2005 at 7:51am
Response.write the type and see what value it gives you


Posted By: dj1811
Date Posted: 22 February 2005 at 3:18pm
Try this,
 
"SELECT password, type FROM [user] WHERE username ='" & strusername & "'"
 
Also you should be careful when choosing names for your database so you do not use reserved words.  User is reserved.  I suggest you use use more unique names such as:
 
user_password, user_type and user_table to avoid using reserved words.
 
Also check if your recordset is empty before you check the password.  Otherwise you will get errors.
 
Like:
<%
IF rsCheckuser.EOF THEN
  'there was no match, go back to login screen
  Response.Redirect("login.asp")
ELSE
  'the code from above
  if LCase(rsCheckuser("type")) = "admin" then
    Response.Redirect "successfulLogin.asp?name=" & strusername
  else
    Response.Redirect("staffframe.asp")
  end if
END IF
%>
 
[EDIT: also watch the syntax so you do not switch from using rsCheckUser to rsCheckuser --  I'm fairly positive this is case sensitive.]


Posted By: elbeardo
Date Posted: 22 February 2005 at 3:40pm
Ok i reverted back to my original one but still with the user.type in the query so i could check the result from Response.write of the type

"SELECT user.password, user.type FROM
user WHERE user.username ='" & strusername & "'"


The script works again (with no redirects for admin or normal users) but the Response.write(rsCheckuser("type")) or Response.write("type") dont seem to be giving me anything....not sure if this is due to the redirect for users to another asp page or what?!

Maybe it will be easier to look at if i post all the code for this
<%

Dim conn, rsCheckuser, filePath, sqlQuery, username, password, strusername

Set conn = Server.CreateObject("ADODB.Connection")
MdbFilePath = Server.MapPath("Project.mdb")
conn.open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & MdbFilePath & ";"



strusername = Request.Form("formUsername")
password = Request.Form("formPassword")




sqlQuery = "SELECT user.password, user.type FROM user WHERE user.username ='" & strusername & "'"

'Query the database
set rsCheckuser = conn.execute(sqlQuery)


If NOT rsCheckuser.EOF Then

If (Request.Form("formPassword")) = rsCheckuser("password") Then

'If password correct then set the session variable to True
Session("passCorrect") = True

'Close Objects before redirecting
conn.Close
set conn = nothing
Set rsCheckuser = Nothing

'redirect
if LCase(rsCheckuser("type")) = "admin" then
Response.Redirect "successfulLogin.asp?name=" & strusername
else
Response.Redirect("staffframe.asp")
end if

End If

'Close Objects
conn.Close
set conn = nothing
Set rsCheckuser = Nothing

'non authorised
Session("passCorrect") = False

'Redirect to the unautorised user page

Response.Redirect"failedLogin.html"
%>

ps sorry about the layout of this code... i dont know how to get it looking pretty when posted


-------------
dude


Posted By: Gullanian
Date Posted: 22 February 2005 at 3:51pm
You probably don't have any values in the type column.  Open up the database and check if you do.


Posted By: elbeardo
Date Posted: 22 February 2005 at 3:55pm
I've just checked again and i definitly do have values in the db type column.

again thanks for all your time everyone


-------------
dude


Posted By: elbeardo
Date Posted: 22 February 2005 at 4:03pm
just posted the code up here for a better view http://mysite.wanadoo-members.co.uk/robspages/login.htm - http://mysite.wanadoo-members.co.uk/robspages/login.htm

and the page it is redirected to if successful http://mysite.wanadoo-members.co.uk/robspages/successfulLogin.htm - http://mysite.wanadoo-members.co.uk/robspages/successfulLogin.htm %20http://mysite.wanadoo-members.co.uk/robspages/successfulLogin.htm%20 -

%20http://mysite.wanadoo-members.co.uk/robspages/login.htm%20 -

-------------
dude


Posted By: Gullanian
Date Posted: 22 February 2005 at 4:23pm
It's not pulling the value out of the database.

Set rsCheckuser = Nothing

Thats your culprit.  Your closing the object before finishing with the values.


Posted By: elbeardo
Date Posted: 22 February 2005 at 4:36pm
Hmmm......... so where should i have the set rsCheckuser = nothing ?
would it go after all of this? or after the "if Lcase" line and before the response.redirect lines?

'redirect
        if LCase(rsCheckuser("type")) = "admin" then
        Response.Redirect "successfulLogin.asp?name=" & strusername
        else
        Response.Redirect("staffframe.asp")


-----------------------------
Ok disreguard that....
im getting the error "Item cannot be found in the collection corresponding to the requested name or ordinal." now on the " if Lcase" line again.
Im going to start crying soon!cheers


-------------
dude


Posted By: Gullanian
Date Posted: 22 February 2005 at 4:42pm
You need to close and reset all objects before every page redirect.  Or alternativly, you could keep it in the same place but put the value in a variable so that you can call on it afterwards.

Basically all objects used must be set to nothing in every case where the page closes or changes.


Posted By: dj1811
Date Posted: 22 February 2005 at 7:13pm
<%
     Dim conn, rsCheckuser, sqlQuery
     Dim strPassword1, strPassword2, strType, strUsername
    
     Set conn = Server.CreateObject("ADODB.Connection")
     conn.open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Project.mdb") & ";"
    
     strUsername = Request.Form("formUsername")
     strPassword1 = Request.Form("formPassword")
     sqlQuery = "SELECT password, type FROM [user] WHERE username ='" & strUsername & "'"
 
     set rsCheckuser = conn.execute(sqlQuery)
    
     If NOT rsCheckuser.EOF Then
           strPassword2 = rsCheckuser.Fields("password")
           strType=LCase(rsCheckuser.Fields("type"))
     END IF
   
     rsCheckuser.Close
     conn.Close
     Set rsCheckuser=Nothing
     Set conn=nothing
 
     If strPassword1 = strPassword2 Then
           Session("passCorrect") = True
           If strType = "admin" then
                 Response.Redirect "successfulLogin.asp?name=" & strUsername
           Else
                 Response.Redirect "staffframe.asp"
           End If
     Else
          Session("passCorrect") = False
          Response.Redirect"failedLogin.html"
     End if
 
    
%>
 
Something like that maybe is what you are trying to ... this way does the same thing and is maybe a tiny bit simpler:
 
<%
     Dim conn, rsCheckuser, sqlQuery
     Dim strPassword, strType, strUsername
    
     Set conn = Server.CreateObject("ADODB.Connection")
     conn.open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Project.mdb") & ";"
    
     strUsername = Request.Form("formUsername")
     strPassword = Request.Form("formPassword")
     sqlQuery = "SELECT type FROM [user] WHERE username ='" & strUsername & "'" AND password='" & strPassword & "'"
 
     set rsCheckuser = conn.execute(sqlQuery)
    
     If rsCheckuser.EOF Then
           strType = LCase(rsCheckuser.Fields("type"))
     END IF
   
     rsCheckuser.Close
     conn.Close
     Set rsCheckuser=Nothing
     Set conn=nothing
 
     SELECT CASE strType
         CASE "admin"
              Session ("passCorrect") = True
              Response.Redirect "successfulLogin.asp?name=" & strUsername
         CASE ""
              Session ("passCorrect") = False
              Response.Redirect "failedLogin.html"
         CASE ELSE
              Session ("passCorrect") = True
              Response.Redirect "staffframe.asp"
     END SELECT
%>
 
That only works if there are no NULL or blank values in they [type] field.


Posted By: elbeardo
Date Posted: 23 February 2005 at 5:17am
Seriously, thanks for your replies (and time)... 
I havent checked that last one as of yet but i found another way of doing the same thing which seems to work....
Would be good if i could get this way to work too though so i will get back to you all on this....
 
Much appreciated,
thanks,
rob


-------------
dude



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net