首先搭建環(huán)境,建立個ip.php.
代碼如下:
<?
error_reporting(0);
function GetIP(){
if($_SERVER['HTTP_CLIENT_IP']){
$onlineip=$_SERVER['HTTP_CLIENT_IP'];
}elseif($_SERVER['HTTP_X_FORWARDED_FOR']){
$onlineip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$onlineip=$_SERVER['REMOTE_ADDR'];
}
return $onlineip;
}
?>
再建立個index.php
代碼如下:
<?php
error_reporting(0);
require 'ip.php';
echo '<hr>'.'Your IP is '.GetIP().'<br>'.'<hr>';
/*echo 'REMOTE_ADDR is '.$_SERVER['REMOTE_ADDR'].'<br>';
echo 'HTTP_CLIENT_IP is '.$_SERVER['HTTP_CLIENT_IP'].'<br>';
echo 'HTTP_X_FORWARDED_FOR is '.$_SERVER['HTTP_X_FORWARDED_FOR'].'<br>';
echo 'HTTP_VIA is '.$_SERVER['HTTP_VIA'];*/
?>
測試
IP顯示正確,客戶端真實IP是218.241.179.50
去掉index.php里面的注釋,使用代理觀察
可以看到REMOTE_ADDR方法抓到了代理IP
HTTP_XFORWARDED_FOR還是抓到了客戶端的真實IP
接下來編輯curl_proxy.php,示例代碼:
<?php
error_reporting(0);
function curl_string ($url,$user_agent,$proxy){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_COOKIEJAR, "d:\cookies.txt");
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('CLIENT-IP:125.210.188.36', 'X-FORWARDED-FOR:125.210.188.36')); //此處可以改為任意假IP
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
$url_page = "http://s4nd.no-ip.org/test/index.php";
$user_agent = "Mozilla/4.0";
$proxy = "http://125.210.188.36:80"; //此處為代理服務(wù)器IP和PORT
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;
?>
訪問curl_proxy.php
122.66.*.*是運行腳本服務(wù)器的IP,這樣就實現(xiàn)了隱藏客戶端真實IP的目的。
有的代理服務(wù)器會被HTTP_VIA方法偵測到使用了代理服務(wù)器,實際上透明代理和高級匿名代理有很大區(qū)別。