zoukankan      html  css  js  c++  java
  • HTTP Referer 防外链

    HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理。

      if (Request.ServerVariables["HTTP_REFERER"] == null || !Request.ServerVariables["HTTP_REFERER"].ToString().Contains(Request.ServerVariables["HTTP_HOST"].ToString()))
    {
    Server.Transfer("Error.aspx");
    }

    下列情况是从浏览器地址栏正常取得Request.ServerVariables("HTTP_REFERER"):
    1) 直接用<a href="">
    2) 用Submit或<input type="image">提交的表单(POST或GET)
    3) 使用JAvaScript提交的表单(POST或GET)
     

    下面我们再看看Request.ServerVariables("HTTP_REFERER")不能正常取值的情况:
    1) 从收藏夹链接
    2) 单击主页或自定义的地址
    3) 在浏览器中直接输地址
    4) 使用JavaScript的Location.href或者是Location.replace()
    5) <%Response.Redirect%>
    6) <%Response.AddHeader%>或者是<mete http-equiv="refresh">转向
    7) 用XML加载地址
    显然,Request.ServerVariables("HTTP_REFERER")在多数情况下是不能正常工作的,下面我们看一个例子:
    ref.asp
    <% 
    response.write "You came from: " & request.servervariables("http_referer") 
    %>
    ref.htm
    <% 
    Response.AddHeader "Refresh", "10;URL=ref.asp" 
    %>
    <meta http-equiv='refresh' content='10;URL=ref.asp'>
    <form method=GET action=ref.asp name=getform> 
    <input type=submit value=' Go there (GET) >> '> 
    <input type=image style='cursor:hand'> 
    </form><p> 
    看看上面的代码会得到什么的结果.
    <form method=POST action=ref.asp name=postform> 
    <input type=submit value=' Go there (POST) >> '> 
    <input type=image style='cursor:hand'> 
    </form><p>
    <a href='ref.asp'>直接链接</a><p>
    <a href='#' onclick='window.location.href="ref.asp";return false;'>Javascript location</a><p>
    <a href='#'onclick='window.location.replace("ref.asp");return false;'>Javascript replace</a><p>
    <a href='#' onclick='document.getform.submit();return false;'>javascript GET</a><p>
    <a href='#' onclick='document.postform.submit();return false;'>javascript POST</a>
    Request.ServerVariables(HTTP_REFERER)的工作方式
  • 相关阅读:
    面向对象的相关知识
    模块的导入
    正怎表达式在爬虫里的应用
    前端~css
    CSS知识点
    初识Html
    Python之路--协程/IO多路复用
    进程
    锁 和 线程池
    操作系统-并发-线程-进程
  • 原文地址:https://www.cnblogs.com/B-bowen/p/4275121.html
Copyright © 2011-2022 走看看