If the referenced sites are relatively small, the "useless" traffic caused by the leeching of external sites puts pressure on the Webmaster (the traffic also requires money ). Anti-leech can be prevented from two aspects: Server and program. Different servers have different judgments. iis needs to install specific anti-leech software. Today we mainly discuss anti-leech protection in programs.
Principles
Currently, handler is widely used for anti-leech protection. Specifically, create a processing class for a specific file request (inherited from IHttpHandler) and configure all the file requests in web. config to point to this class. Then, in the class, determine whether the previous request exists and points to the domain name of our site. If so, it is considered not a leeching and returns a real file. Otherwise, the error image is returned.
Let's imagine if another website references images on our site and applies them to an article. There is a personal request for the website's article. The user's request is sent to their server, and the server on their site returns html for browser resolution. When the browser parses our image address, it will send a request for this image to our website. Because our program sets this request to be forwarded to our specific class for processing, the program checks whether the request before this request is blank (apparently it only requests other images without requests, so of course there is no previous request). If it is blank, an error image is returned. After understanding the above process, it is easy to know why the image in the webpage that requested our site is correctly displayed, and the user shows the page that requested the request, so of course there is a previous access record in it. When the browser parses the image on our site, it returns the correct result. I don't know if everyone understands this?
Code Implementation
First, create a class that inherits from IHttpHandler. Here we call it the ForbiddenInvaliteDownload class:
CodeCopy codeThe Code is as follows: public bool IsReusable
{
Get {return true ;}
}
Public void ProcessRequest (HttpContext context)
{
If (null! = Context. Request. UrlReferrer)
{
Context. Response. Expires = 0;
Context. Response. Clear ();
Context. Response. ContentType = "image/jpg ";
Context. Response. WriteFile (context. Request. PhysicalPath );
Context. Response. End ();
}
Else
{
Context. Response. Expires = 0;
Context. Response. Clear ();
Context. Response. ContentType = "text/html ";
Context. Response. Write ("leeching ");
Context. Response. End ();
}
}
There are very few codes, and you will understand it at a glance. It is to use context. Request. UrlReferrer to determine whether the previous Request exists. If it exists, it is regarded as legal; otherwise, it is invalid.
This class cannot forward all jpg requests. We need to configure webconfig and configure it in System. Web:
CodeCopy codeThe Code is as follows:
<Add verb = "*" path = "*. jpg" type = "Namespace. ForbiddenInvaliteDownload, Namespace"/>
</HttpHandlers>
It is also mentioned that by default, IIS does not send requests for JPG files, but directly obtains them. Therefore, we also need to configure in iis to forward all jpg requests to our processing program, rather than directly obtaining them by iis by default. Configuration
So far, your website has been able to prevent jpg files from being stolen. Of course, other zip files can also be implemented. You can process a common class and determine the type based on the request suffix for operations. Below is:
Is it over?
The above method does not prevent downloading software such as thunder. You can download the software by entering these addresses in thunder. If another website uses your .zip File Link, you can download it directly. How can this problem be solved? Now I want to add session verification.
If the user accesses your download page, set session ["visited"] = "true" in the load, and then add a session verification code in the download. The Code is as follows (note that you need to inherit
System. Web. SessionState. IRequiresSessionState to use the session ):
Code
Copy codeThe Code is as follows: if (null! = Context. Request. UrlReferrer & context. Session ["visited"] = "true ")
{
Context. Response. Expires = 0;
Context. Response. Clear ();
Context. Response. ContentType = "image/jpg ";
Context. Response. WriteFile (context. Request. PhysicalPath );
Context. Response. End ();
}
Else
{
Context. Response. Expires = 0;
Context. Response. Clear ();
Context. Response. ContentType = "image/jpg ";
Context. Response. WriteFile (context. Request. PhysicalApplicationPath + "Images/2.jpg ");
Context. Response. End ();
}
In this way, when debugging, we can call thunder to download this image and find that there is no value in the session. Of course, we will download the error image.