我可以在页面加载时设置http_referer吗?

时间:2012-06-15 03:44:58

标签: php header set spoofing referer

我想知道是否可以为正在加载的页面设置referer标头?

所以设置referer而不是加载页面。

我有这个代码,我想执行并完美地运行,但我也想将引用URL附加到此。

$data = file_get_contents('http://Domain');
$regex = '/<div\b[^>]+?\bid\s*=\s*"MyID"[^>]*>(?:((?:[^<]++|<(?!\/?div\b[^>]*>))+)|(<div\b[^>]*>(?>(?1)|(?2))*<\/div>))?<\/div>/i';
preg_match($regex,$data,$match);
//var_dump($match);
echo $match[0];

我想从另一个页面加载div并将引用者设置为“Domain”

谢谢!

2 个答案:

答案 0 :(得分:5)

您可以设置引荐来源和许多其他内容,但仍然使用file_get_contents()首选卷曲,因为它更快,更可自定义。

<?php 
$url = 'example.com';
$referer = 'Domain';


$header[] = "Accept: text/xml,application/xml,application/json,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";

$opts = array('http'=>array('method'=>"GET",
                            'header'=>implode('\r\n',$header)."\r\n".
                            "Referer: $referer\r\n",
                            'user_agent'=> "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8"));

$context = stream_context_create($opts);

$data = file_get_contents($url, false, $context);
?>

或者你可以使用curl:

<?php 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_REFERER, $referer);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$data = curl_exec($curl);
?>

答案 1 :(得分:0)

您应该能够像使用curl时一样使用内容...通过设置CURLOPT_RETURNTRANSFER,您可以使用数据......

$ch = curl_init();
curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_REFERER => 'your referer value'
  CURLOPT_URL => 'http://Domain'
));

$data= curl_exec($ch);
curl_close($ch);

$regex = '/<div\b[^>]+?\bid\s*=\s*"MyID"[^>]*>(?:((?:[^<]++|<(?!\/?div\b[^>]*>))+)|(<div\b[^>]*>(?>(?1)|(?2))*<\/div>))?<\/div>/i';
preg_match($regex,$data,$match);
//var_dump($match);
echo $match[0];