开发者

comparing http_referer against http_host

开发者 https://www.devze.com 2023-01-26 23:06 出处:网络
Hi I need to check whether the http_referer is the same site as the current site. I have the following code

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
0

精彩评论

暂无评论...
验证码 换一张
取 消