Use PHP to forge the Referer code to break through the Network Disk and prohibit external connections

Source: Internet
Author: User

For example, the file http://img.namipan.com/downfile/da333ee178bdad6531d1ec1540cf86277c116b6300887600/02.Paid%20In%20Full.mp3 I put in the disk

This address cannot be downloaded directly or played in the webpage player. Click Download To go to a download page. To download or play this file directly, it is necessary to forge a referfer and cheat the server. referfer is a link from the nano disk.

The initial method is to use the PHP built-in function stream_context_create.

CodeAs follows:

Function referfile ($ URL, $ refer = "){
$ Opt = array ('http' => array ('header' => "Referer: $ refer "));
$ Context = stream_context_create ($ OPT );
Return file_get_contents ($ URL, false, $ context );
}

However, this code has a disadvantage. file_get_contents actually reads the files from the other server to its own server and sends them to the browser, which not only increases the pressure on its own server, but also slows down the download response, this is not a good solution.

Later, I read the manual to relieve the pressure on my server and confuse the other server. Modify the referer information in the header.

the revised Code is as follows:
$ url = "scheme
$ urlarr = explode ("/", $ URL );
$ domain = $ urlarr [0]; // break down the domain name
$ GetFile = str_replace ($ urlarr [0], ", $ URL );
$ content = @ fsockopen ("$ domain", 80, $ errno, $ errstr, 12); // connect to the server of the other party first
If (! $ Content) {// an error message is displayed when the connection fails.
die ("sorry, unable to connect to $ domain .");
}< br> fputs ($ content, "get $ GetFile HTTP/1.0 \ r \ n");
fputs ($ content, "Host: $ domain \ r \ n ");
fputs ($ content," Referer: $ domain \ r \ n "); // counterfeit referfer
fputs ($ content, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) \ r \ n ");

While (! Feof ($ content )){
$ TP. = fgets ($ content, 128); read the header information, which will include location: http: // xxx/xxx.mp3. What we need is this address.
}
$ Arr = explode ("\ n", $ TP );
$ Arr1 = explode ("Location:", $ TP );
$ Arr2 = explode ("\ n", $ arr1 [1]); // break down location: the address following
Header ('content-type: Application/force-download ');
Header ("location:". $ arr2 [0]);
Fclose ($ content );

OK.

This original address: http://img.namipan.com/downfile/3a7c64518d46d986283eab73175a8b119305a76480b89200/Equilibrium-Turis_Fratyr-02-Wingthors_Hammer.mp3

After conversion:

Http://mms.music.krmcn.com/mms.music/namipan/img ~~ /3a7c64518d46d986283eab73175a8b119305a76480b89200/Equilibrium-Turis_Fratyr-02-Wingthors_Hammer.mp3

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.