View Full Version : Response.Redirect Question
Thatguy2001au
08-06-2003, 11:49 AM
Hi
Ok, this question may sound a little stupid and very simple, but i was just wondering about how to redirect to different folders in a website. My prob:
i have a page called 'Products.asp' at the root of my site. I also have a folder at the root of my site called 'Store'. I have a page in the folder 'Store' called 'login.asp'. Within login.asp i want to redirect the user back to the products page at the root of my site. How would i write this???
response.redirect(????)
Also, if i want to redirect login.asp back to it's self, would i just need to write 'response.redirect("login.asp") or do i need some sort of path aswell?
Also, since i am using the Response object, do i need to use 'Reponse.Buffer = true" anywhere??? or will the page still work without it? What does it do anyway???
Your help would be greatly appreciated.
thatguy
Roelf
08-06-2003, 12:40 PM
Originally posted by Thatguy2001au
i have a page called 'Products.asp' at the root of my site. I also have a folder at the root of my site called 'Store'. I have a page in the folder 'Store' called 'login.asp'. Within login.asp i want to redirect the user back to the products page at the root of my site. How would i write this???
response.redirect("/products.asp")
Originally posted by Thatguy2001au
Also, if i want to redirect login.asp back to it's self, would i just need to write 'response.redirect("login.asp")
yes
Originally posted by Thatguy2001au
or do i need some sort of path aswell?
no
Originally posted by Thatguy2001au
Also, since i am using the Response object, do i need to use 'Reponse.Buffer = true" anywhere??? or will the page still work without it? What does it do anyway???
response.buffer = true is used to prevent writing to the clients browser. If anything is written to the browser, the response.redirect doesn't work anymore
writing the buffered output to the browser is done by using response.flush, or at the end of the asp-code (automatically)
Roelf
Also, if i want to redirect login.asp back to it's self, would i just need to write 'response.redirect("login.asp")
Or you can use the more dynamic and renameproof version (code once, run forever ...)
response.redirect(Request.ServerVariables ("SCRIPT_NAME"))
But when do you ever need that ? You're executing a script, that at some point redirects to itself ??
+ about buffering --> normally you shouldn't have any output if you redirect later on.
If you got such a script where you need both things, then there is probably something wrong with your coding logic. (I think)
whammy
08-07-2003, 01:27 AM
Actually raf, I do that all the time.
That logic works when you post a form to itself, at any rate. When you redirect back to the page, it won't have any of the values it's checking for, so it will display whatever the "first thing" is you want the page to do.
However regarding buffering the output in this type of scenario, you're right! But the way I do normal ASP, I don't display ANY HTML until all the server-side processing (at least the logic) is done, using subroutines. I never have a problem that way.
Actually raf, I do that all the time.
That logic works when you post a form to itself, at any rate. When you redirect back to the page, it won't have any of the values it's checking for, so it will display whatever the "first thing" is you want the page to do.
I often use multipurpose pages (like form that are posted to itself), but you don't need to use a redirect for that. You just set a flag to some value, each time the page is parsed, and on top of the page (or before the formfields where processing is needed), you check for that flag and perform the actions you need.
I think i just saw people doing it like that and copied it ... I'm not sure but i think that using flags and not redirecting allows more flexability. But i'll keep it in mind as an option and maybe run into situations where redirecting has some advantages.
Thatguy2001au
08-07-2003, 09:18 AM
Thanks Guys
You have all helped me do what i need to do.
The reason why I wanted to redirect the login page back to itself is because when i submit the login form on that page, i submit it back to itself and a check is made to see if the user has clicked the submit button. If they have, then a function and some other asp code is used to run a stored procedure to see if the user exists in the database. Now, instead of going to an error page if the user does not exist, i just redirect the login page back to itself with a URL string attached which has the error message in it. The login page checks to see if a string is attached and if it is, then it will display the error message on the same page just above the login form so the user knows what went wrong.
Maybe there is a better way of doing things, but i just wanted to try and minimize the amount of pages i need and it was the best i could come up with. If anyone has any other ideas, then please let me know, i'm just tryin to learn as much as i can.
Thanks again guys
Thatguy
Well, this is how i would do it.
When you build the form tag, you have
response.write("<form action=""" & Request.ServerVariables ("SCRIPT_NAME") &""" method=""POST"" ... etc ... >")
This posts the page to itself.
Inside this page, you have a check at the top of it. Like
dim flag
if Len(request.form("loginstate"))=0 then ' so the first time that the loginpage is requested, this will be true
here you have a welcome message or whatever. Some text that only needs to e displayed the first time.
flag = 0
else 'this means the page is requested for the second time --> the loginform is posted
flag = 1
dim error
if Len(request.form("username") > 0 AND Len(request.form("username") > 0 then 'in this line, you have all the conditions that need to be met
error = 0
your code to run the stored procedure and proces the returned values
if there are errors here (record not found, incorrect pasword etc), you set error = 1 and usererror="User not found" or whatever
if there are no errors --> redirect
else ' this means the form wasn't completed right, so we are going to redisplay it, with erromessages
' the simplest way is to display them at the top, but it's much more userfriendly to display them
' alongside the faulty formfields. So i assume you have a table or so here
error = 1
if Len(request.form("username")) = 0
usererror=(" You did not fill in a username.")
end if
same for the password
end if
end if
if flag = 0 or (flag=1 and error = 1) then 'so on firrst parsing or if there's an error
response.write("<form action=""" & Request.ServerVariables ("SCRIPT_NAME") &""" method=""POST"" ... etc ... >")
response.write("<table ....> ...")
response.write("<tr><td>")
if Len(usererror) > 0 then 'this means there is an error for the uservalue
response.write("<font color=""red"">"& usererror & "</font>")
end if
response.write("</td>")
your code for the textfield username. Same for the password.
your code to display the submitbutton and close the table
response.write("<input type=""hidden"" value=""" & flag & """ name=""loginstate""></form>") 'so the second time the page is requested,
the check at the top will return true and the from will be processed
your code to display the submitbutton and close the table
end if
You see? At the top, you check out if the form was corectly filled in (or if it's the first time it is loaded) And depending on that, you run some querys and proces them and redirect, or youstore an errormessage in a variable.
Then you see if a form needs to be displayed (on firt request or if the form isn't filled in correct) and you just place the errormessage in front in the faulty formfield.
At the end of the form, you store the flagvalue in a hidden formfield. So if the form is sumitted, the first check will return false and the form will be processed.
You see?
You can even make this dynamical and run a 'for each loop' on the formscollection to check if all required fields are filled in, if all numeric fields contain numerical values etc. For this, you only need to compose the formfieldnames like strrequser (str = string req = required user=forfield)
Then you can have
for each ffield in request.form
if Mid(ffield,4,3)="req" then
if Len(request.form(ffield)) = 0 then
problem = 1
error = error & "<br />No value given for " & right(ffield, (Len(ffield)-6)) & ". This is a required field"
else
select case Left(ffield, 3)
case str
your string checks
case num
if IsNumeric(ffield)=False then
problem = 1
error = error & "<br />No numerical value given for " & right(ffield, (Len(ffield)-6)) & ". This is a numerical and required field"
your other checks. Maybe some regex's
case dat
if IsDate(ffield)=False then
problem = 1
error = error & "<br />No date value given for " & right(ffield, (Len(ffield)-6)) & ". This is a date and required field"
....
end select
end if
else
your checks for non-required fields
end if
next
You can then print the erromessage on top of the form, or you could make an array of the erromessage variable and print the erromessages in front of the formfields or whatever.
Maybe someone out here wrote some formfield-checking functions you can use
Thatguy2001au
08-07-2003, 11:01 AM
Thanks Raf
You have given me some great ideas. They will come in very handy.
Thanks again
Thatguy
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.