首頁  >  文章  >  後端開發  >  php curl如何設定ip

php curl如何設定ip

藏色散人
藏色散人原創
2020-08-27 10:04:013526瀏覽

php curl設定ip的方法:先透過getip方法取得訪客ip;然後使用「getreferer」方法取得訪客來源;最後實作設定頁面限制ip和來源存取即可。

php curl如何設定ip

推薦:《PHP影片教學》 

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.csdn.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, &#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中文網其他相關文章!

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