php 偽造HTTP_REFERER頁面URL來源的三種方法


php獲取當前頁面的前一個頁面URL地址,即當前頁面是從哪個頁面鏈接過來的,可以使用$_SERVER['HTTP_REFERER'];

但是$_SERVER['HTTP_REFERER']也是可以被偽造欺騙的,有三種方法可以偽造和欺騙$_SERVER['HTTP_REFERER']

注:window平台 使用phpstudy集成環境 nginx 此方法失效 ,apache 正常,其他平台版未測試

第一種方法:file_get_contents

$url = "http://localhost/test/test.php";
$refer="http://www.aa.com";
$opt=array('http'=>array('header'=>"Referer: $refer"));
$context=stream_context_create($opt);
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;

file_get_contents中stream_context_create就偽造來源的重要參數了。

 

第二種方法:CURL

$url = "http://localhost/test/test.php"; // 請求的頁面地址
$refer="http://www.aa.com";  //偽造的頁面地址
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_REFERER,$refer);
curl_exec ($ch);
curl_close ($ch);

 

第三種方法:fsockopen

$url="http://localhost/test/test.php";
$target = "http://www.manongjc.com/";
/** sockopen 偽造 網站來源地址
 * @parem $url 要訪問的頁面地址
 * @parem $target 偽造來源頁面
 * @parem $port 網站端口 默認 80
 * @parem 頁面腳本執行時間 默認 30 s
*
*/ function referer($url,$target,$port=80,$t=30) { $info=parse_url($url); $fp = fsockopen($info["host"], $port, $errno, $errstr, $t); if(!$fp) { echo "$errstr($errno)".PHP_EOL; } else { $out = "GET ".$info['path']." HTTP/1.1".PHP_EOL; $out .= "Host: ".$info["host"].PHP_EOL; $out .= "Referer: ".$target.PHP_EOL; $out .= "Connection: Close".PHP_EOL; $out .= PHP_EOL; fwrite($fp, $out); while(!feof($fp)) { echo fgets($fp); // 發送 head 請求頭信息 } fclose($fp); } } //函數調用 referer($url,$target);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM