PHP-written resource download anti-leech sharing

Source: Internet
Author: User

I have been writing a PHP anti-leech external resource download handler in the past few days. I just completed the compilation last night and encountered some problems. I will not go into detail here;
The following are self-written simple PHP anti-leech processing classes (recompile them into class files for later improvement );

Copy codeThe Code is as follows:
<? Php
/**
*
* Anti-leech external resource download processing class
*
* @ Link http://jb51.net
*
*/
Class BurglarDow {
/**
* Initial license Download Status
* @ Var allow
* @ Access private
*/
Private $ allow = false;
/**
* Initial
* @ Var dowUrl
* @ Access private
*/
Private $ dowUrl = null;
/**
* Initial origin Domain Name
* @ Var RemoteUrl
* @ Access private
*/
Private $ RemoteUrl = null;
/**
* List of domain names used for initial license Resources
* @ Var allowUrl
* @ Access private
*/
Private $ allowUrl = array ();
/**
* Initial jump address
* @ Var Location
* @ Access private
*/
Private $ Location = null;

Public function _ construct ($ dowUrl, $ Location, array $ allowUrl ){
// Initial
$ This-> dowUrl = $ dowUrl;
// List of domain names used for initial licensed Resources
$ This-> allowUrl = $ allowUrl;
// Initial jump address
$ This-> Location = $ Location;

$ This-> RemoteUrl = @ parse_url ($ _ SERVER ['HTTP _ referer']); // obtain the origin Domain Name
If (! Is_array ($ this-> RemoteUrl ))
Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location:". $ this-> Location );

If (isset ($ this-> RemoteUrl ['host']) {
If (in_array ($ this-> RemoteUrl ['host'], $ this-> allowUrl) {// determines whether the domain name is licensed.
$ This-> allow = true; // The download license status is true.
}
}
Unset ($ this-> allowUrl, $ this-> RemoteUrl); // release the memory variable
}

/**
* Anti-leech resource download
* @ Access public
* @ Return mixed
*/
Public function dow (){
$ FileInfo = get_headers ($ this-> dowUrl, 1); // obtain the remote file header information

If (true ===$ this-> allow) {// determine whether to permit resource download
// Determine whether the configuration file exists
If (is_file ('config. ini ')){
$ FileCon = parse_ini_file ('config. ini ');
} Else {
$ FileName = basename ($ FileInfo ['content-location']);
$ FileConStr = "FileName = {$ FileName} \ r \ nFileUrl = {$ FileInfo ['content-location']} \ r \ nFileSize = {$ FileInfo ['content-length' ]} ";
$ Handle = fopen ('config. ini ', "wb"); // create a file if the Config. ini file does not exist.
If (fwrite ($ handle, $ FileConStr) = FALSE) {// write data to a file
Echo "File creation failed ...";
}
Fclose ($ handle); // close an opened file pointer
$ FileCon = parse_ini_file ('config. ini ');
}
If (! Empty ($ this-> dowUrl )){
$ Fp = @ fopen ($ this-> dowUrl, "rb"); // read files in binary mode
If (! $ Fp)
Exit ("Download a mistake. \ n ");

// Output Remote Resources
Header ("Content-type: text/html; charset = UTF-8 ");
Header ('content-Description: File Transfer ');
Header ('content-Type: application/octet-stream ');
Header ('content-Disposition: attachment; filename = '. $ FileCon ['filename']);
Header ("Accept-Ranges: bytes ");
Header ('content-Transfer-Encoding: binary ');
Header ('expires: 0 ');
Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 ');
Header ('pragma: public ');
Header ('content-Length: '. $ FileCon ['filesize']);
While (! Feof ($ fp )){
Set_time_limit (0); // sets the maximum file execution time.
Echo fread ($ fp, 1024); // output file
Flush (); // output buffer
Ob_flush (); // content in the output buffer
}
Fclose ($ fp );
} Else {
Header ("HTTP/1.1 404 Not Found ");
}
} Else {
Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location:". $ this-> Location );
}
}
}
// Remote resource address
$ DowUrl = 'HTTP: // dldir1.qq.com/qqfile/qq/qq5.1/10055/qq5.1.exe ';
// Jump address
$ Location = 'HTTP: // jb51.net ';
// List of Licensed domain names
$ AllowUrl = array (
'Jb51. net ',
);
$ BurglarDow = new BurglarDow ($ dowUrl, $ Location, $ allowUrl );
$ BurglarDow-> dow ();

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.