您好,登錄后才能下訂單哦!
本篇文章為大家展示了HTTP_REFERER函數(shù)在php中的使用方法,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
利用php的http_referer函數(shù)來(lái)判斷用戶的來(lái)路,這是比較簡(jiǎn)單的,實(shí)例代碼如下:
復(fù)制代碼 代碼如下:
<?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>
下面是我們讓用戶不知道我們的來(lái)路處理,實(shí)例代碼如下:
復(fù)制代碼 代碼如下:
<?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";
}
這不就行了?
不過(guò)很奇怪的是,www.jb51.net 的頁(yè)面抓下來(lái)是亂碼(除了http頭),這是為什么?難道是因?yàn)橛昧薵zip之類壓縮?
復(fù)制代碼 代碼如下:
<?php
$host = "www.jb51.net";
$html = file_get_contents("http://".$host);
$fp = file_put_contents("hao123.html",$html);
echo "done";
?>;
但這樣抓的就沒(méi)問(wèn)題,再來(lái)分析開始抓的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 ,原來(lái)壓縮了的,長(zhǎng)度14567字節(jié)了,用第二種方法抓,原來(lái)沒(méi)壓縮的html是71143字節(jié),原來(lái)file_get_contents還可以自動(dòng)解壓縮.
php實(shí)例二,代碼如下:
復(fù)制代碼 代碼如下:
<?php
$host = '127.0.0.1';
$target = '/2.php';
$referer = 'https://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);
}
?>
另一個(gè)2.php文件很簡(jiǎn)單,只是寫上一行讀取當(dāng)前的HTTP_REFERER服務(wù)器值的代碼即可,如下:
復(fù)制代碼 代碼如下:
<?php
echo "<hr />";
echo $_SERVER["HTTP_REFERER"];
?>
上述內(nèi)容就是HTTP_REFERER函數(shù)在php中的使用方法,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。