【问题标题】:comparing http_referer against http_host比较 http_referer 和 http_host
【发布时间】:2010-11-24 13:49:44
【问题描述】:

嗨 我需要检查http_referer是否和当前站点是同一个站点。

我有以下代码

Dim strReferer As String

strReferer = Request.ServerVariables("HTTP_REFERER")
If strReferer.Contains(Request.ServerVariables("HTTP_HOST")) then
   'do task
End If

这是抛出一个错误说 - “对象引用未设置为对象的实例。”并将 if 行标记为有问题的代码行。

有什么想法我哪里出错了吗?

我的解决方案:

strReferer = "" & Request.ServerVariables("HTTP_REFERER")

意味着字符串总是有一个值,即使它什么都没有。

【问题讨论】:

    标签: asp.net contains request.servervariables


    【解决方案1】:

    因为HTTP_REFERER 并不总是被填充 - 只有当你点击了一个链接。因此,如果您直接浏览到某个页面,该页眉将为空。

    【讨论】:

      【解决方案2】:

      Request.ServerVariables("HTTP_REFERER") 可能是null,因此在分配变量时应检查这一点。

      If Not String.IsNullOrEmpty(Request.ServerVariables("HTTP_REFERER"))
          'do your stuff
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-14
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        • 2016-04-08
        • 1970-01-01
        • 2014-06-12
        • 2014-09-16
        相关资源
        最近更新 更多