HTTP_REFERER not always there

T

tshad

Why would HTTP_REFERER not be there in the Page_Load event?

I am using it to determine whether a page was called from a particular page.

I am doing:

sTest = Request.ServerVariables("HTTP_REFERER")
if (sTest = "") ORELSE (sTest.SubString(sTest.LastIndexOf("/")+1) <>
"job_posting_new2.aspx") then
newPosition = new Position
session.Remove("newPosition")
else
newPosition = session("newPosition")
end if

I was originally doing:

if (sTest.SubString(sTest.LastIndexOf("/")+1)

But if HTTP_REFERER wasn't there, I would get an error:

Object reference not set to an instance of an object.

Not sure why.

Thanks,

Tom
 
S

Shawn

Are you using Request.Redirect or Server.Transfer in the previous page?
Don't think you will be getting a HTTP_REFERER value then, only if you came
to the page as a result of clicking on a link.

Shawn
 
J

Jack Li

Hi Tshad,

I think you should check the null reference before referencing it. You
should not depends on this variable. The variable is sent by the browser, it
is not a must there is always one as there is a number of browser out there
(IE, Firefox, etc). Also, some security software may block the HTTP_REFERER.

Regards,
Jack Li
MVP (ASP.NET)
 
P

Patrice

This could be not available (this is optional and some browser my block this
for privacy purpose) plus it won't be there if the the user have the page in
its favorites...

You could keep track of the previous page in the session variable as you
seems to do...
 
J

Juan T. Llibre

Hi, tshad.

re:
I was originally doing:
if (sTest.SubString(sTest.LastIndexOf("/")+1)
But if HTTP_REFERER wasn't there, I would get an error:
Object reference not set to an instance of an object.
Not sure why.

Because there's no LastIndexOf a null object.

If the page hasn't been referred from another page, HTTP_REFERER is null.
 
T

tshad

Patrice said:
This could be not available (this is optional and some browser my block
this
for privacy purpose) plus it won't be there if the the user have the page
in
its favorites...

You could keep track of the previous page in the session variable as you
seems to do...

But the problem with that is I would need to put that in all my pages.

Mainly, I have a few pages that are connected, such as an Application may
take 5 pages to finish and I want to make sure that the use doesn't go
directly to page 3 from a Browser entry. I want them to go to Page 1 first
and push the next button (or previous button after the first page) to
navigate the pages. To do this I need to know that page 3 got there from
either page 2 or page 4. The referer would be the logical place to look,
but if some browsers are preventing that, then I can't very well use that.

It really makes that ServerVariable virtually useless. If you can't depend
on it, how can you use it.

Tom
 
G

George

ServerVariables only reflect what browser has sent to the server (plus some server info).

If browser sent the HTTP_REFERER then ServerVariable will have that info, if it did not send that then nobody in a world will get it.

On real life server (on interent) the HTPP_REFERER is missing from 30% of all requests. So you will have to simply deal with that fact.




George.

Patrice said:
This could be not available (this is optional and some browser my block
this
for privacy purpose) plus it won't be there if the the user have the page
in
its favorites...

You could keep track of the previous page in the session variable as you
seems to do...

But the problem with that is I would need to put that in all my pages.

Mainly, I have a few pages that are connected, such as an Application may
take 5 pages to finish and I want to make sure that the use doesn't go
directly to page 3 from a Browser entry. I want them to go to Page 1 first
and push the next button (or previous button after the first page) to
navigate the pages. To do this I need to know that page 3 got there from
either page 2 or page 4. The referer would be the logical place to look,
but if some browsers are preventing that, then I can't very well use that.

It really makes that ServerVariable virtually useless. If you can't depend
on it, how can you use it.

Tom
 
P

Patrice

If you have only 5 pages where you need to keep track of that it looks like
not that cumbersome. Another option is to put this in some general place
that is always executed (perhaps beginrequest if you don't have such a
general place for now).
 
T

tshad

Patrice said:
If you have only 5 pages where you need to keep track of that it looks
like
not that cumbersome. Another option is to put this in some general place
that is always executed (perhaps beginrequest if you don't have such a
general place for now).

It is not really a cumbersome problem, but a problem with making sure that
page 2 is only loaded after page 1 or 3, 3 is only loaded after 2 or 4, etc.

I also need to make sure that page one, when not entered from page 2 reads
my data from Sql and my old session variables, if they exist, are cleared..
If it comes from page 2 then get the data from my session variables.

This would have been perfect with the referer.

I am going to have to put in all of my pages on entry, an entry in my
session variables that says what this page is. That way the next page I go
to will know what the previous page was.

I would have assumed that if I did a redirect, I would be able to tell where
the page came from.

Apparently, that is not the case.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top