PDA

View Full Version : HTTP_REFERER (show referring URL)



Netherland
12-17-2006, 05:19 PM
I have a web page on which I would like to show the referring domain. I recently learned that HTTP_REFERER can be used to show the complete url by using:

strURL = Request.ServerVariables("HTTP_REFERER")

However, I was wondering if it's possible to somehow trim out all parts of the URL other than the domain itself.

In other words - if someone is referred to my web page from e.g.

http://www.dynamicdrive.com/forums/newthread.php?do=postthread&f=10
or:
http://www.dynamicdrive.com/?epl=387687fswoodwi

.. then I would like to only show www.dynamicdrive.com on my web page.

Does anyone know how to go about this? Any help will be greatly appreciated!

djr33
12-18-2006, 12:31 AM
In PHP, I believe there is another variable for just the domain. (something like HTTP_HOST, or something)

I don't know ASP, though.

Sure... not hard to split a string at a certain point.

But not sure on the exact syntax for ASP, sorry.

Very possible, though.

mwinter
12-18-2006, 02:21 AM
In PHP, I believe there is another variable for just the domain.

Not for the referrer. That must be obtained by parsing the Referer header.



(something like HTTP_HOST, or something)

That is the name of an element in the $_SERVER super-global, and, like all element names beginning "HTTP_", it is the value of a HTTP header (Host, in this case).


Exactly how to implement this depends on the language used to interact with ASP.

JScript should look something like this:



var referrer = Request.ServerVariables('HTTP_REFERER'),
matches = /^[^:\/?#]+:\/\/([^\/?#]+)/.exec(referrer),
referringDomain;

if (matches) referringDomain = matches[1];

The VBScript equivalent should look something like:



Dim referrer, pattern, matches, referringDomain
Set pattern = New RegExp

referrer = Request.ServerVariables('HTTP_REFERER')
pattern.Pattern = "^[^:/?#]+://([^/?#]+)"
Set matches = pattern.Execute(referrer)

If matches.Length = 1 Then
referringDomain = matches(0).Value
End If

I can't be sure though: I can't test it and I don't even know if the Matches collection has a Length property. My reference (written by Microsoft) alludes to a single read-only property, but doesn't mention what it's called; I can only assume it's supposed to be the number of elements in the collection.

Keep in mind that the Referer header doesn't need to exist and its value can be spoofed.

Anyway, I hope that helps,
Mike

Netherland
12-18-2006, 01:07 PM
Hello Mike,

Thanks for your elaborate reply. I tried out the VBSCRIPT you suggested, but unfortunately couldn't get it to work. If I had a bit more of a clue about scripting, I may have been able to :rolleyes:

I just got a reply on another forum, with a very simple code that seems to work perfectly:

Dim strURL, tmparray, domain
strURL = Request.ServerVariables("HTTP_REFERER")
tmparray = Split(strURL, "/")
domain = tmparray(2)

I will use this for the time being. But thanks again for your input. Much appreciated!

Alex



"Keep in mind that the Referer header doesn't need to exist and its value can be spoofed."

Thanks for the warning. Will look into this!

mwinter
12-19-2006, 05:18 PM
I tried out the VBSCRIPT you suggested, but unfortunately couldn't get it to work.

Yes, I did think there would be problems, but it was the best I could suggest at the time.



Dim strURL, tmparray, domain
strURL = Request.ServerVariables("HTTP_REFERER")
tmparray = Split(strURL, "/")
domain = tmparray(2)

That would break if the Referer header was missing. This can be avoided by testing the bounds of the array. I would correct it, but it seems that regular expression performance, or accessing the resulting objects, is poor in VBScript.



Dim segments, referrer, referringDomain

referrer = Request.ServerVariables("HTTP_REFERER")
segments = Split(referrer, "/")

If UBound(segments) >= 2 Then
referringDomain = segments(2)
End If

Mike

shoppersusa.com
12-19-2006, 11:52 PM
Dim segments, referrer, referringDomain

referrer = Request.ServerVariables("HTTP_REFERER")
segments = Split(referrer, "/")

If UBound(segments) >= 2 Then
referringDomain = segments(2)
End If



can be changed to



Dim segments, referrer, referringDomain

referrer = Request.ServerVariables("HTTP_REFERER")
segments = Split(referrer, "/")
IF isarray(segments) then
IF ubound(segments) >= 2 then
referringDomain = segments(2)
END IF
End If

mwinter
12-20-2006, 12:15 AM
referrer = Request.ServerVariables("HTTP_REFERER")
segments = Split(referrer, "/")
IF isarray(segments) then
IF ubound(segments) >= 2 then
referringDomain = segments(2)
END IF
End If


As far as I can see, the Split function returns an array even if its first argument is Empty. There's nothing wrong with using the IsArray function, but it doesn't seem necessary, either.

Mike

Netherland
12-20-2006, 08:45 PM
Many thanks for your latest insights, guys. Will do my best to try and understand and test what you are saying over the coming weekend!