


How to use PHP to forge referer to break through the network disk and prohibit external connections
This article mainly introduces how to use PHP to forge referer to break through the network disk to prohibit external connections. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
General domestic network In order to control traffic, external links to files in the network disk will be prohibited. Clicking on the link to the file in the network disk will generally lead to a special download page, and you must download the file from this page.
For example, the file I put in the nanodisk http://img.namipan.com/downfile/da333ee178bdad6531d1ec1540cf86277c116b6300887600/02.Paid In Full.mp3
This address cannot be downloaded directly or on the web page Play it in the player and click download to go to a download page. If you want to be able to directly download or play this file, you need to forge the referfer and deceive the server. The referfer is a link from the nanodisk.
The first method used is to use PHP’s built-in function stream_context_create.
The code is as follows:
function referfile($url, $refer=”) { $opt=array('http'=>array('header'=>”Referer: $refer”)); $context=stream_context_create( $opt); return file_get_contents($url,false, $context); }
But this code has a shortcoming. file_get_contents actually reads the files from the other party's server to its own server and then sends them to the browser. Not only does it increase the pressure on your own server, but the download response speed is also very slow. This is not a good solution.
Later I read the manual again, not only to reduce the pressure on my own server, but also to confuse the other party's server. Just modify the Referfer information in the header.
The revised code is as follows:
<?php $url="img.namipan.com/downfile/da333ee178bdad6531d1ec1540cf86277c116b6300887600/02.Paid%20In%20Full.mp3";//这里的url要过滤掉http:// $urlarr=explode("/",$url); $domain=$urlarr[0];//分解出域名 $getfile=str_replace($urlarr[0],",$url); $content = @fsockopen("$domain", 80, $errno, $errstr, 12);//先连接上对方的服务器 if (!$content){//无法链接就提示错误信息 die("对不起,无法连接上 $domain 。"); } fputs($content, "GET $getfile HTTP/1.0\r\n"); fputs($content, "Host: $domain\r\n"); fputs($content, "Referer: $domain\r\n");//伪造referfer fputs($content, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n"); while (!feof($content)) { $tp.=fgets($content, 128);将头部信息读取出来,里面将包含:Location:http://xxx/xxx.mp3,我们要的就是这个地址。 } $arr=explode("\n",$tp); $arr1=explode("Location: ",$tp); $arr2=explode("\n",$arr1[1]);//分解出Location:后面的地址 header('Content-Type:application/force-download'); header("location:".$arr2[0]); fclose($content);
OK, the purpose is achieved.
This original address: http://img.namipan.com/downfile/3a7c64518d46d986283eab73175a8b119305a76480b89200/Equilibrium-Turis_Fratir-02-Wingthors_Hammer.mp3
After conversion:
http:// mms.music.krmcn.com/mms.music/namipan/img~~/3a7c64518d46d986283eab73175a8b119305a76480b89200/Equilibrium-Turis_Fratir-02-Wingthors_Hammer.mp3
Attached is another piece of implementation code:
The most original PHP code to crack network disk restrictions on external links. If you want to learn and research, you can continue to read. If you want to use the code directly and need to improve it, this code is for reference only.
Create a new file file.php. The following parameter is the target address of the referfer that needs to be forged. For example: file.php/http://www.xxx.xxx/xxx.mp3
<? $url=str_replace('/file.php/','',$_SERVER["REQUEST_URI"]);//得出需要转换的网址。这里我就偷懒,不做安全检测了,需要的自己加上去 $downfile=str_replace(" ","%20",$url);//替换空格之类,可以根据实际情况进行替换 $downfile=str_replace("http://","",$downfile);//去掉http:// $urlarr=explode("/",$downfile);//以"/"分解出域名 $domain=$urlarr[0];//域名 $getfile=str_replace($urlarr[0],'',$downfile);//得出header中的GET部分 $content = @fsockopen("$domain", 80, $errno, $errstr, 12);//连接目标主机 if (!$content){//链接不上就提示错误 die("对不起,无法连接上 $domain 。"); } fputs($content, "GET $getfile HTTP/1.0\r\n"); fputs($content, "Host: $domain\r\n"); fputs($content, "Referer: $domain\r\n");//伪造部分 fputs($content, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n"); while (!feof($content)) { $tp.=fgets($content, 128); if (strstr($tp,"200 OK")){ //这里要说明一下。header的第一行一般是请求文件的状态。具体请参照HTTP 1.1状态代码及其含义hi.baidu.com/110911/blog/item/21f20d2475af812ed50742c5.html这里 是正常的文件请求状态,只需直接转向就可以。其他状态的继续执行程序 header("Location:$url"); die(); } } //302 转向,大部分的防盗链系统都是先判断referfer,对了的话再转向真实的地址。下面就是获取真实的地址。 $arr=explode("\n",$tp); $arr1=explode("Location: ",$tp);//分解出Location后面的真时地址 $arr2=explode("\n",$arr1[1]); header('Content-Type:application/force-download');//强制下载 header("location:".$arr2[0]);//转向目标地址 die(); ?>
115 network disk picture external link test (not done by me): Original address: http:// u.115.com/file/f3b7c9046
External link address: http://115.pp.ru/f3b7c9046/02.jpg
115 Network disk external link test 2 (not done by me ): http://gg.org.ru/115.php/Extraction Code/xxx
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to PHP Chinese net!
Related recommendations:
Implementing the display of article classes in CMS through php
Analysis on str_replace replacement vulnerability in php
The above is the detailed content of How to use PHP to forge referer to break through the network disk and prohibit external connections. For more information, please follow other related articles on the PHP Chinese website!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor