Home  >  Article  >  Backend Development  >  How to use the php HTTP_REFERER function_PHP tutorial

How to use the php HTTP_REFERER function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:02:521010browse

Use the http_referer function in the PHP tutorial to determine where the user is coming from. This is easy,
Example

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!

Now we let users not know our origin
Example

]$host = "www.123cha.com";
$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";
}[/code]

Isn’t this enough?

But the strange thing is,
www.hao123.com
The captured page is garbled (except for the http header). Why is this? Is it because gzip or other compression is used?

[code]$host = "www.zhutiai.com";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;[/code]


But there’s no problem if you catch it this way.

Let’s analyze the http header we started to capture
[code]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[/code]

As expected, there is this sentence, Content-Encoding: gzip
It turned out to be compressed and has a length of 14567 bytes,
Using the second method to capture, the original uncompressed html is 71143 bytes, and it turns out that file_get_contents can also automatically decompress.


Example 2

$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:

echo "


";
echo $_SERVER["HTTP_REFERER"];
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445343.htmlTechArticleUse the http_referer function of the PHP tutorial to determine the user's origin. This is simple. Example if (isset($ _SERVER[HTTP_REFERER])) { print The page you were on previously was {$_SERVER[HT...
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