欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php中HTTP_REFERER函数用法实例

程序员文章站 2022-12-08 14:31:16
本文实例分析了php中http_referer函数用法。分享给大家供大家参考。具体分析如下: 利用php的http_referer函数来判断用户的来路,这是比较简单的,实...

本文实例分析了php中http_referer函数用法。分享给大家供大家参考。具体分析如下:

利用php的http_referer函数来判断用户的来路,这是比较简单的,实例代码如下:

复制代码 代码如下:
<?php
   if (isset($_server['http_referer'])) {
     print "the page you were on previously was {$_server['http_referer']}<br />";
   } else {
     print "you didn't click any links to get here<br />";
      }
    ?>
<a href="refer.php">click me!</a>

下面是我们让用户不知道我们的来路处理,实例代码如下:
复制代码 代码如下:
<?php
$host = "www.jb51.net";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
        echo "$errstr ($errno)<br>;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";
}

这不就行了?

不过很奇怪的是,www.jb51.net 的页面抓下来是乱码(除了http头),这是为什么?难道是因为用了gzip之类压缩?

复制代码 代码如下:
<?php
$host = "www.jb51.net";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;

但这样抓的就没问题,再来分析开始抓的http头:

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

果然有这句,content-encoding:gzip ,原来压缩了的,长度14567字节了,用第二种方法抓,原来没压缩的html是71143字节,原来file_get_contents还可以自动解压缩.

php实例二,代码如下:

复制代码 代码如下:
<?php
$host = '127.0.0.1';
$target = '/2.php';
$referer = '//www.jb51.net'; //伪造http_referer地址
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr($errno)<br />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);
}
?>

另一个2.php文件很简单,只是写上一行读取当前的http_referer服务器值的代码即可,如下:
复制代码 代码如下:
<?php
echo "<hr />";
echo $_server["http_referer"];
?>

希望本文所述对大家的php程序设计有所帮助。