Home >Backend Development >PHP Tutorial >Example of usage of HTTP_REFERER function in php, phphttp_referer_PHP tutorial

Example of usage of HTTP_REFERER function in php, phphttp_referer_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:351123browse

Example of usage of HTTP_REFERER function in php, phphttp_referer

This article analyzes the usage of HTTP_REFERER function in php with examples. Share it with everyone for your reference. The specific analysis is as follows:

Use PHP's http_referer function to determine the user's origin. This is relatively simple. The example code is as follows:

Copy code The code is as follows:
if (isset($_SERVER['HTTP_REFERER'])) {
Print "The page you were on previously was {$_SERVER['HTTP_REFERER']}
";
} else {
Print "You didn't click any links to get here
";
        }
?>
Click me!

The following is how we prevent users from knowing our origin. The example code is as follows:
Copy code The code is as follows:
$host = "www.jb51.net";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr ($errno)
;n";
}else{
$request = "
GET/HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */ "."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";

fputs ($fp, "$request");
while (!feof($fp))
{
$res[] = fgets($fp,1024);
}
$html = join("",$res);
fclose ($fp);
$fp = file_put_contents("123cha.html",$html);
echo "done";
}

Isn’t this enough?

But what is very strange is that the page of www.jb51.net is garbled (except for the http header). Why is this? Is it because it uses gzip or other compression?

Copy code The code is as follows:
$host = "www.jb51.net";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;

But there is no problem in catching it this way. Let’s analyze the http header that we started to catch:

HTTP/1.1 200 OK Date: Wed, 31 Aug 2005 00:59:36 GMT Server: Apache/1.3.27 Cache-Control: max-age=1296000 Expires: Thu, 15 Sep 2005 00:59:36 GMT Last-Modified: Mon, 29 Aug 2005 13:56:00 GMT Accept-Ranges: bytes Connection: close Content-Type: text/html Content-Encoding: gzip Content-Length: 14567

Sure enough, there is this sentence, Content-Encoding: gzip, it turns out to be compressed, the length is 14567 bytes, using the second method to capture, the original uncompressed html is 71143 bytes, it turns out that file_get_contents can also automatically decompress.

php example two, the code is as follows:

Copy code The code is as follows:
$host = '127.0.0.1';
$target = '/2.php';
$referer = 'http://www.bkjia.com'; //Fake HTTP_REFERER address
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr($errno)
n";
}
else{
$out = "
GET $target HTTP/1.1
Host: $host
Referer: $referer
Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp)){
echo fgets($fp, 1024);
}
fclose($fp);
}
?>

The other 2.php file is very simple, just write a line of code to read the current HTTP_REFERER server value, as follows:
Copy code The code is as follows:
echo "
";
echo $_SERVER["HTTP_REFERER"];
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/915427.htmlTechArticleExamples of usage of HTTP_REFERER function in php, phphttp_referer This example analyzes the usage of HTTP_REFERER function in php. Share it with everyone for your reference. The specific analysis is as follows: Using php's http...
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