Anti-leech measures based on HTTP_REFERER in php (stream_context_create)

Source: Internet
Author: User

If you want to take measures to break through the Anti-leech protection, you need to consider using HTTP_REFERER. The corresponding variable in the PHP script is $ _ SERVER ['HTTP _ referer'], which stores the value of HTTP_REFERER.

Because direct access to the target URL resource has been blocked by the above anti-leech measures, we need a gateway-like device to obtain it. To put it bluntly, you can compile a PHP script that has packaged the HTTP header.

The following is a simple function implementation:
Copy codeThe Code is as follows:
Function getRemoteFile ($ url, $ refer = ''){
$ Option = array (
'Http' => array (
'Header' => "Referer: $ refer ")
);
$ Context = stream_context_create ($ option );
Return file_get_contents ($ url, false, $ context );
}

This is a simple function. Its function is to forge a Referer (using the stream_context_create function) and then obtain the data of the other party (using file_get_contents, You need to enable allow_url_fopen ).

If you want to be "complex", you can use the sockets extension, which is not covered here.

In addition, a regular function is provided to obtain the host name.
Copy codeThe Code is as follows:
Function getHost ($ url ){
$ Result = preg_match ('/^ http: \/([\ d | \ w | \.] +) \ //', $ url, $ matches );
If (sizeof ($ matches)> = 2 ){
Return $ matches [1];
} Else {
Return null;
}
}

Further extensions can be encapsulated into scripts, such as calling

Http: // 127.0.0.1/proxy. php? Url = http:// I .am/imgcan get some of the anti-leech links (and then use Javascript to replace all the image links ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.