首頁  >  文章  >  後端開發  >  php 實作使用curl模擬ip和來源進行存取的方法

php 實作使用curl模擬ip和來源進行存取的方法

墨辰丷
墨辰丷原創
2018-05-23 11:21:104146瀏覽

本篇文章主要介紹php 實作使用curl模擬ip和來源進行存取的方法,有興趣的朋友參考下,希望對大家有幫助。

對於限制了ip和來源的網站,使用正常的存取方式是無法存取的。本文將介紹一種方法,使用php的curl類別實作模擬ip和來源,造訪那些限制了ip和來源的網站。

1.設定頁面限制ip與來源存取

server.php

<?php

$client_ip = getip();
$referer = getreferer();

$allow_ip = &#39;192.168.1.100&#39;;
$allow_referer = &#39;http://www.jb51.net&#39;;

if($client_ip==$allow_ip && strpos($referer, $allow_referer)===0){
  echo &#39;allow access&#39;;
}else{
  echo &#39;deny access&#39;;
}


// 获取访问者ip
function getip(){
  if(!empty($_SERVER[&#39;HTTP_CLIENT_IP&#39;])){
    $cip = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
  }elseif(!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])){
    $cip = $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;];
  }elseif(!empty($_SERVER[&#39;REMOTE_ADDR&#39;])){
    $cip = $_SERVER[&#39;REMOTE_ADDR&#39;];
  }else{
    $cip = &#39;&#39;;
  }
  return $cip;
}

// 获取访问者来源
function getreferer(){
  if(isset($_SERVER[&#39;HTTP_REFERER&#39;])){
    return $_SERVER[&#39;HTTP_REFERER&#39;];
  }
  return &#39;&#39;;
}

?>

2.使用curl正常存取

<?php
function doCurl($url, $data=array(), $header=array(), $timeout=30){

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

  $response = curl_exec($ch);

  if($error=curl_error($ch)){
    die($error);
  }

  curl_close($ch);

  return $response;

}

// 调用
$url = &#39;http://www.example.com/server.php&#39;;
$response = doCurl($url);

echo $response;
?>

返回deny access

#3.使用curl模擬ip和來源進行存取

模擬來源

curl_setopt($ch, CURLOPT_REFERER, '來源');

模擬ip

#curl_setopt ($ch, CURLOPT_HTTPHEADER, array('CLIENT-IP: 模擬ip','X-FORWARDED-FOR: 模擬ip'));

#完整程式碼如下:

<?php
function doCurl($url, $data=array(), $header=array(), $referer=&#39;&#39;, $timeout=30){

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

  // 模拟来源
  curl_setopt($ch, CURLOPT_REFERER, $referer);

  $response = curl_exec($ch);

  if($error=curl_error($ch)){
    die($error);
  }

  curl_close($ch);

  return $response;

}

// 调用
$url = &#39;http://www.example.com/server.php&#39;;
$data = array();

// 设置IP
$header = array(
  &#39;CLIENT-IP: 192.168.1.100&#39;,
  &#39;X-FORWARDED-FOR: 192.168.1.100&#39;
);

// 设置来源
$referer = &#39;http://www.jb51.net/&#39;;

$response = doCurl($url, $data, $header, $referer, 5);

echo $response;
?>

返回allow access

以上就是本文的全部內容,希望對大家的學習有所幫助。


相關推薦:

PHP基礎知識回顧_php基礎

#PHP正確解析UTF-8字串技巧應用_php基礎

#php實作圖片上傳、剪下功能_php技巧

以上是php 實作使用curl模擬ip和來源進行存取的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn