Solved

Server.Transfer and HTTP_REFERER

Posted on 2001-08-24
17
1,244 Views
Last Modified: 2007-12-19
I'm using IIS 5.x and Internet Explorer 4.x (and 5.5).  I've noticed that, when I redirect a page with Response.Redirect(x), Request.ServerVariables("HTTP_REFERER") gets updated properly.  However, if I use Server.Transfer(x), "HTTP_REFERER" still points to the page that called the Server.Transfer function.

Example:
login.asp
---------
Click submit, it sends me to processlogin.asp
Now HTTP_REFERER = login.asp

processlogin.asp
-----------
Invalid user name, return to calling page, login.asp (Call Server.Transfer(Request.SeverVariables("HTTP_REFERER"))
Now HTTP_REFERER = processlogin.asp

login.asp
---------
User re-enters information and clicks submit, processlogin.asp is brought back up
Now HTTP_REFERER = processlogin.asp

If I use Response.Redirect("login.asp") instead of the Server.Transfer method, it works, but then the fields that were submitted get lost, so I cannot repopulate the text boxes from the Request.Form() collection (and Response.Redirect isn't as well supported since it's run client-side and it's slower than Server.Transfer for that same reason).

Is there any way to accurately determine the calling page when using Server.Transfer to redirect the user to a different page?  I assume that it's impossible to get HTTP_REFERER to update properly, but is there a server-side variable (maybe something like Server.Referer or something) that would have this information?

Thanks,
Alex Morris
0
Comment
Question by:Egore
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6423140
You could store it in the Session temporarily before transfer, e.g.,


Session("Referer") = Request.ServerVariables("HTTP_REFERER")
server.Transfer("page.asp")
0
 
LVL 3

Author Comment

by:Egore
ID: 6423163
I've thought about doing something like that, but it would require me storing the Request.ServerVariables("URL") & "?" & Request.ServerVariables("QUERY_STRING") (I think those are the two server variables that I would need to concatenate) every time that I call Server.Transfer().  That's a real pain considering the number of times that I need to use Server.Transfer().  This is basically my back-up plan / last resort.

I guess I could just create a wrapper function that does this...  But I'd prefer a cleaner / less hacked solution, if you know what I mean...
0
 
LVL 2

Expert Comment

by:prokni
ID: 6423192
I don't get this paret

<<fields that were submitted get lost, so I cannot <<repopulate the text boxes from the Request.Form() <<collection

Do you mean that you can not recieve fields value that were submittted to the first page?

Paymon
0
 
LVL 2

Expert Comment

by:prokni
ID: 6423196
I don't get this paret

<<fields that were submitted get lost, so I cannot <<repopulate the text boxes from the Request.Form() <<collection

Do you mean that you can not recieve fields value that were submittted to the first page?

Paymon
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6423216
Well, note that when you call Server.Transfer, you are basically just calling a function as if it were in the existing ASP page itself.  That is why all of ASP's intrinsic objects, like the Request object, in the page being transferred to, retain the same values as the original, calling page.
0
 
LVL 3

Author Comment

by:Egore
ID: 6423231
Azra, quite true, but does ASP store the URL that is requested via Server.Transfer() anywhere, or is that "an exercise left to the programmer"?

Prokni, what I was saying is that, if you submit some data via a form and then use Response.Redirect, the Request object is cleared.  If you use Server.Transfer, the Request object is not cleared.  Therefore, if I Redirect instead of Transfer-ing, I lose all of the data that the user submitted (unless I throw it all into the QueryString which is really ugly / prone to users messing with it).

I appreciate the help,
Alex Morris
0
 
LVL 2

Expert Comment

by:prokni
ID: 6423256
Sorry still your question is not clear for me.
From your second comments, I get that you need to have
Request.ServerVariables("URL")
& "?" & Request.ServerVariables("QUERY_STRING") of first page before server.transfer get called.
may i ask why you need. What are you trying to do?
You have access to all collection on the transfered page.
You have access to all request.Form and ...
What are you trying to do? Why do you need to have the first HTTP_REFERER  value?
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 6423258
Well, it stores the url in HTTP_REFERER, but you want which asp page is calling this other asp page.  AFAIK, there is no property to determine that, but then again, I dont know everything there is to know about ASP.  

Should that property not exist, you will have to do something to let you distinguish between which script called this page being transferred to.  Via some variable set in the Session object, or creating a separate page to transfer to based on which asp page is calling for the transfer, etc.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:raizon
ID: 6423303
From my understanding Server.Transfer was brought in to transfer control of asp from one asp page to another and still retain all objects as the existed on the sending page.  Which is why the HTTP_REFERER doesn't get updated and the Request object is not cleared.
0
 
LVL 3

Author Comment

by:Egore
ID: 6423327
Alright, so I'm thinking that I'll do as Azra suggested and simply store the name of the ASP page in a Session object before calling Server.Transfer().  With a simple wrapper function this can be controlled fairly easily.  So, the big question is, what's the command to get the name of the script that is currently executing?  Obviously, Request.ServerVariables("SCRIPT_NAME") (I think that's the variable) will be wrong.  Is there a routine in ASP that will tell me the name of the currently executing script?  If not, I'll wind up having to go the cheap way and hard-code the file name into each script, and I'd rather not do that...
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6423350
Request.ServerVariables("SCRIPT_NAME") should give you the page that called FOR the transfer, e.g.,


Caller.asp
<%
Server.Transfer("Callee.asp")
%>


Callee.asp
<%
'this should write "Caller.asp" to the browser
Response.Write Request.ServerVariables("SCRIPT_NAME")
%>


This is what you were hoping for is it not?  To be able to determine which script is calling the second page?
0
 
LVL 3

Author Comment

by:Egore
ID: 6423410
Azra, I don't think that would work on my situation (the database is griping about max connections allowed so I cannot currently log back on to test, so I'm crippled for ~1-30 more minutes, argh!), here's the basics of what I'm doing:

1.) manage_users.asp - click on a link to "edit_user.asp?mode=add"
2.) edit_user.asp - realizes that this user does not have access to add a new user.  Calls: Server.Transfer( R.SV("HTTP_REFERER") )
3.) manage_users.asp - user is a moron and clicks on the same link to "edit_user.asp?mode=add"
4.) edit_user.asp - realizes that this user does not have access to add a new user.  Calls: Server.Transfer( R.SV("HTTP_REFERER") )
5.) That Transfer call bounces the user back to edit_user.asp because HTTP_REFERER is out of sync, sits there calling itself until the world comes to an end...

I've been thinking about it, and I'm pretty sure that, after the first time we bounce back to "manage_users.asp", the SCRIPT_NAME will be set to "edit_user.asp" and will therefore wind up in an infinite loop again.

(I know, I should do a check and hide the "Add User" link on the manage_users.asp page in the first place so that this cannot happen -- but there are plenty of logical places where I will need to be able to re-direct back to the calling page: invalid data entry being very high on that list.)

Thanks,
Alex Morris
0
 
LVL 3

Author Comment

by:Egore
ID: 6423420
Thanks Azra, this definately answers my question (not as cleanly as I'd like, but as cleanly as possible with ASP/IIS 5).  I'll probably either do a wrapper to Server.Transfer() or, in most cases, there is only one page that could possible call a page that would need this functionality, so I can hard-code the page to return to.

Thanks a ton for the help,
Alex Morris
0
 
LVL 5

Expert Comment

by:raizon
ID: 6423429
Request.ServerVariables("SCRIPT_NAME") will give you the name of the current page(script)

Caller.asp
<%
scriptname = Request.ServerVariables("SCRIPT_NAME")
scriptname = Right(scriptname,InstrRev(scriptname,"/")-1)
Session("Referer") = scriptname
Server.Transfer("Callee.asp")
%>

Callee.asp
<%
Response.Write Session("Referer")
%>

should do what you are wanting.
0
 
LVL 3

Author Comment

by:Egore
ID: 6423456
raizon, unfortunately, all of the Request.ServerVariables collection becomes virtually useless once you do a Transfer.  I'm slowly beginning to wonder if the drawbacks to Server.Transfer are far worse than those of Response.Redirect...

- Alex
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6423489
>>all of the Request.ServerVariables collection becomes virtually useless


No, they dont become useless, they are just the same as the page that is doing the transfer.  When it says transfer, it really means it...its transferring the entire context of the calling page.  Think of the code that is executing in callee.asp, as being copied/pasted right where you have written Server.Transfer("Callee.asp") on the calling page.  Therefore, each time from Callee.asp that you ask for HTTP_REFERER, you should get back the HTTP_REFERER of Caller.asp, and each time you ask for SCRIPT_NAME from Callee.asp, you should get back the SCRIPT_NAME for Caller.asp, namely, "Caller.asp"
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6423495
Server.Transfer can also be thought of as a dynamic include, but that can be called from anywhere within the page.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now