当前位置:首页 > PHP教程 > php函数 > 列表

php HTTP_REFERER函数的使用用法

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-18 17:20:01 浏览: 评论:0 

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

  1. <?php 
  2.    if (isset($_SERVER['HTTP_REFERER'])) { 
  3.      print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />"
  4.    } else { 
  5.      print "You didn't click any links to get here<br />"
  6.       } 
  7.     ?> 
  8. <a href="refer.php">Click me!</a> 

下面我们让用户不知道我们的来路处理,实例代码如下:

  1. <?php 
  2. $host = "www.phpfensi.com"
  3. $referer = "http://".$host
  4. $fp = fsockopen ($host, 80, $errno$errstr, 30); 
  5. if (!$fp){ 
  6.         echo "$errstr ($errno)<br>;n"
  7. }else
  8. $request = " 
  9. GET / HTTP/1.1 
  10. Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."
  11. Referer: http://$host 
  12. Accept-Language: zh-cn 
  13. Accept-Encoding: gzip, deflate 
  14. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) 
  15. Host: $host 
  16. Connection: Close" 
  17. ."rnrn"
  18.  
  19.  
  20. fputs ($fp"$request"); 
  21. while (!feof($fp)) 
  22.    $res[] = fgets($fp,1024); 
  23. $html = join("",$res); 
  24. fclose ($fp); 
  25. $fp = file_put_contents("123cha.html",$html); 
  26. echo "done";//开源代码phpfensi.com 

这不就行了?

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

  1. <?php 
  2. $host = "www.phpfensi.com"
  3. $html = file_get_contents("http://".$host); 
  4. $fp = file_put_contents("hao123.html",$html); 
  5. echo "done"
  6. ?>; 

但这样抓的就没问题,再来分析开始抓的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实例二,代码如下:

  1. <?php 
  2. $host = '127.0.0.1'
  3. $target = '/2.php'
  4. $referer = 'http://www.phpfensi.com'; //伪造HTTP_REFERER地址 
  5. $fp = fsockopen($host, 80, $errno$errstr, 30); 
  6. if (!$fp){ 
  7. echo "$errstr($errno)<br />n"
  8. }  
  9. else
  10. $out = " 
  11. GET $target HTTP/1.1 
  12. Host: $host 
  13. Referer: $referer 
  14. Connection: Closernrn"; 
  15. fwrite($fp$out); 
  16. while (!feof($fp)){ 
  17. echo fgets($fp, 1024); 
  18. fclose($fp); 
  19. ?> 

另一个2.php文件很简单,只是写上一行读取当前的HTTP_REFERER服务器值的代码即可,如下:

  1. <?php 
  2. echo "<hr />"
  3. echo $_SERVER["HTTP_REFERER"]; 
  4. ?>

Tags: HTTP_REFERER函数 PHP伪造来路

分享到: