方法:1、利用“file_get_contents($url,false,$context)”函數(shù);2、CURL方式,利用curl_init()、curl_setopt()、curl_exec()等函數(shù);3、利用fsockopen()函數(shù)。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php獲取當(dāng)前頁面的前一個頁面URL地址,即當(dāng)前頁面是從哪個頁面鏈接過來的,可以使用$_SERVER['HTTP_REFERER'];
但是$_SERVER['HTTP_REFERER']也是可以被偽造欺騙的,有三種方法可以偽造和欺騙$_SERVER['HTTP_REFERER']
注:window平臺 使用phpstudy集成環(huán)境 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就偽造來源的重要參數(shù)了。
第二種方法: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 偽造 網(wǎng)站來源地址 * @parem $url 要訪問的頁面地址 * @parem $target 偽造來源頁面 * @parem $port 網(wǎng)站端口 默認(rèn) 80 * @parem 頁面腳本執(zhí)行時間 默認(rèn) 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); // 發(fā)送 head 請求頭信息 } fclose($fp); } } //函數(shù)調(diào)用 referer($url,$target);
推薦學(xué)習(xí):《PHP視頻教程》