Home  >  Article  >  Backend Development  >  referer PHP fake referer example code

referer PHP fake referer example code

WBOY
WBOYOriginal
2016-07-29 08:38:511540browse

The complete program will be given directly here. You can modify it yourself for specific applications.
The example I gave here is very simple. In fact, many applications can be developed from this example. For example, hiding the real URL address... Hehe, just analyze it yourself
Create a new file file.php here. 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

Copy the code The code is as follows:


$url=str_replace('/file.php/', '',$_SERVER["REQUEST_URI"]);//Get the URL that needs to be converted. I'm being lazy here and don't do security checks. I'll add what I need
$downfile=str_replace(" ","%20",$url);//Replace spaces and the like, you can replace them according to the actual situation
$downfile =str_replace("http://","",$downfile);//Remove http://
$urlarr=explode("/",$downfile);//Use "/" to decompose the domain name
$domain =$urlarr[0];//Domain name
$getfile=str_replace($urlarr[0],'',$downfile);//Get the GET part in the header
$content = @fsockopen("$domain", 80, $errno, $errstr, 12);//Connect to the target host
if (!$content){//If the link cannot be connected, an error will be prompted
die("Sorry, cannot connect to $domain.");
}
fputs($content, "GET $getfile HTTP/1.0rn");
fputs($content, "Host: $domainrn");
fputs($content, "Referer: $domainrn");//Fake part
fputs ($content, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)rnrn");
while (!feof($content)) {
$tp.=fgets($content, 128);
if (strstr($tp,"200 OK")){ //Something to explain here. The first line of the header is generally the status of the requested file. For details, please refer to HTTP 1.1 status codes and their meanings hi.baidu.com/110911/blog/item/21f20d2475af812ed50742c5.html This is the normal file request status, just redirect it directly. Continue executing the program in other states
header("Location:$url");
die();
}
}
//302 redirection, most anti-hotlink systems first determine the referfer, and then redirect to the real one if it is correct the address of. The following is to obtain the real address.
$arr=explode("n",$tp);
$arr1=explode("Location: ",$tp);//Decompose the real-time address behind Location
$arr2=explode("n",$ arr1[1]);
header('Content-Type:application/force-download');//Force download
header("location:".$arr2[0]);//Redirect to the target address
die() ;
?>


This program can only be used for the anti-hotlinking system that uses referer to determine whether it is hotlinking. If other special methods are used to prevent hotlinking, this estimate will not apply.

The above introduces the referer PHP fake referer example code, including the referer content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn