首頁  >  文章  >  後端開發  >  如何透過php 使用curl模擬ip和來源進行訪問

如何透過php 使用curl模擬ip和來源進行訪問

jacklove
jacklove原創
2018-06-08 17:46:172274瀏覽

對於限制了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.csdn.net&#39;;if($client_ip==$allow_ip && strpos($referer, $allow_referer)===0){    echo &#39;allow access&#39;;
}else{    echo &#39;deny access&#39;;
}// 获取访问者ipfunction 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正常存取

<?phpfunction 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, &#39;来源&#39;);

模擬ip

curl_setopt($ch, CURLOPT_HTTPHEADER, array(&#39;CLIENT-IP: 模拟ip&#39;,&#39;X-FORWARDED-FOR: 模拟ip&#39;));

完整程式碼如下:

<?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.csdn.net/&#39;;$response = doCurl($url, $data, $header, $referer, 5);

echo $response;
?>

返回allow access

本篇說明了php 使用curl模擬ip和來源進行存取的相關知識,更多相關內容請關注php中文網。

相關推薦:

透過mysql 轉換NULL資料方法

#關於php 函數使用可變數量的參數的相關內容

如何透過php 呼叫新浪呼叫新浪來產生短連結

#

以上是如何透過php 使用curl模擬ip和來源進行訪問的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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