程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php中HTTP_REFERER函數用法實例,phphttp_referer

php中HTTP_REFERER函數用法實例,phphttp_referer

編輯:關於PHP編程

php中HTTP_REFERER函數用法實例,phphttp_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 = 'http://www.bkjia.com'; //偽造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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved