Hi I need to check whether the http_referer is the same site as the current site.
I have the following code
Dim strReferer As String
strReferer = Request.ServerVariables("HTTP_REFERER")
If strReferer.Contains(Request.ServerVariables("HTTP_HOST")) then
'do task
End If
This is throwing up an error saying - "Object reference not set to an instance of an object." 开发者_运维知识库and flagging the if line as the offending line of code.
Any ideas where I'm going wrong?
MY SOLUTION:
strReferer = "" & Request.ServerVariables("HTTP_REFERER")
Means the string always has a value even if it's nothing.
Because HTTP_REFERER
is not always populated - only if you have clicked a link. So if you browse directly to a page, that header will be empty.
It's possible for Request.ServerVariables("HTTP_REFERER")
to be null
, so you should check for this when assigning the variable.
If Not String.IsNullOrEmpty(Request.ServerVariables("HTTP_REFERER"))
'do your stuff
精彩评论