.htm格式网页中的图片如何防止盗链?现在被一些网站用<img src=http://我站域名/images/xxx.jpg>盗链了,有什么办法防止吗?网站用的是虚拟主机自己不能设置IIS.

解决方案 »

  1.   

    http://www.ouaox.cn/index.php?9670-1.html
      

  2.   

    主要思路是用Request.ServerVariables收集得到HTTP_REFERRER,然后根据这个变量的值判断链接是否来自外部,阻止非法链接。 
    首先,我们需要对图片做如下引用: 
    <img src="/images/getimg.asp?FName=pic.jpg"> 
    对getimg.asp我们做如下处理: 
    <% 
    Option Explicit 
    dim Server_Link,FilePath 
    Server_Link=request.ServerVariables("HTTP_REFERRER") 
    Server_Link=mid(Server_Link,InStr(Server_Link,".")+1) 
    Server_Link=left(Server_Link,InStr(Server_Link,"/")-1) 
    If Server_Link="code-123.com" then 
    FilePath="图片文件夹地址" + Request.QueryString("FName") 
    Else 
    FilePath="/images/非法链接.jpg" 
    End If 
    Response.Redirect(FilePath) 
    %>
    可实现防盗链效果。
      

  3.   

    回帖是一种美德!在CS里using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Globalization;
    using System.IO;
    namespace HttpHandler
    {
        public class ImageHandler:IHttpHandler
        {
            #region IHttpHandler 成员
            public void ProcessRequest(HttpContext context)
            {
                HttpRequest req = context.Request;
                string IMG_path = req.PhysicalPath;
                string contentType;
                if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)
                {
                    if (CultureInfo.InvariantCulture.CompareInfo.Compare(req.Url.Host, req.UrlReferrer.Host, CompareOptions.IgnoreCase) != 0)
                    {
                        IMG_path = context.Server.MapPath("~/images/error.gif");
                    }
                    contentType = GetContentType(IMG_path);
                    if (File.Exists(IMG_path))
                    {
                        context.Response.StatusCode = 200;
                        context.Response.ContentType = contentType;
                        context.Response.WriteFile(IMG_path);
                    }
                    else
                    {
                        context.Response.StatusCode = 404;
                        context.Response.Status = "无法找到你请求的文件";
                    }
                }
            }
            public bool IsReusable
            {
                get { return true; }
            }
            #endregion
            private string GetContentType(string path)
            {
                string extension = Path.GetExtension(path);
                string type;
                switch (extension)
                {
                    case ".gif":
                        type = "image/gif";
                        break;
                    case ".jpg":
                        type = "image/Jpeg";
                        break;
                    case ".png":
                        type = "image/png";
                        break;
                    default:
                        type = "";
                        break;
                }
                return type;
            }
        }
    }
    web.config 文件里配置 <add verb="*" path="*.jpg" type="HttpHandler.ImageHandler, HttpHandler"/>
     <add verb="*" path="*.gif" type="HttpHandler.ImageHandler, HttpHandler"/>
     <add verb="*" path="*.png" type="HttpHandler.ImageHandler, HttpHandler"/>
      

  4.   

    现在问题是网站都是.html格式的文件,已生成静态网页,不是.aspx的,有办法吗??
      

  5.   

    现在问题是网站都是.html格式的文件,已生成静态网页,不是.aspx的,有办法吗??
      

  6.   

    6楼的方法是可以的,具体的参考下通过IhttpHandler实现图片验证码
      

  7.   

    不能操作IIS和不是动态页,很难实现
      

  8.   

    用IhttpHandler image url="xxx.aspx?"图片帖成这样的路径 xx.aspx 继承IhttpHandler用流的方式输出图片
      

  9.   

    <img src=http://我站域名/picture.aspx?path=xxx.jpg> picture.aspx继承IhttpHandler用流的方式输出图片
    不管你是html还是aspx页 都可以由picture.aspx 接管jpg picture.aspx.cs可以对来访地址进行判断 空 or!我站域名 输出error.jpg
      

  10.   

    盗链接很好的,给你增加外链了,SEO了,我恨不得大家都来盗链我的网站呢。