使用(yòng)CDN後,獲取真實IP 辦法

發表日期:2012.08.17    訪問人(rén)數:2129

Supesite使用(yòng)CDN後獲取真實IP 辦法

成都制作網頁公司—include/main.inc.php文件,這(zhè)部分(fēn)

if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$_SGLOBAL['onlineip'] = $_SERVER['REMOTE_ADDR'];
}

替換成


function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i]))
{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$_SGLOBAL['onlineip'] = get_real_ip();
Discuz!使用(yòng)CDN後獲取真實IP辦法


nclude/common.inc.php
找到如下(xià)代碼:


if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}

替換爲:

 

function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i]))
{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$onlineip = get_real_ip();

DZ注冊用(yòng)戶IP和(hé)邀請碼擁有者IP相同的(de)解決辦法

方法A:


register.php 200行

showmessage('register_invite_iperror');
改爲
//showmessage('register_invite_iperror');

方法B:
在include/common.inc.php文件中把

代碼:


if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}


替換成:

代碼:
$onlineip=getenv('HTTP_X_FORWARD_FOR');

--------------------

做(zuò)了(le)CDN的(de)ASP網站如何取到用(yòng)戶真實IP程序


function   checkip(checkstring)'用(yòng)正則判斷IP是否合法
dim   re1
set   re1=new   RegExp
re1.pattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
re1.global=false
re1.Ignorecase=false
checkip=re1.test(checkstring)
set   re1=nothing
end   function


function   get_cli_ip()'取真實IP函數,先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 REMOTE_ADDR
dim client_ip
if checkip(Request.ServerVariables("HTTP_CLIENT_IP"))=true then
         get_cli_ip = checkip(Request.ServerVariables("HTTP_CLIENT_IP"))
else
         MyArray = split(Request.ServerVariables("HTTP_X_FORWARDED_FOR"),",")
         if ubound(MyArray)>=0 then
                   client_ip = trim(MyArray(0))
                   if checkip(client_ip)=true then
                            get_cli_ip = client_ip
                            exit function
                   end if
         end if
         get_cli_ip = Request.ServerVariables("REMOTE_ADDR")
end if
end   function


ASP.NET獲取IP的(de)6種方法
服務端:

//方法一
HttpContext.Current.Request.UserHostAddress;

//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

//方法四(無視代理(lǐ))
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


客戶端:

//方法五
var ip = '{C}';
alert("Your IP address is "+ip);

//方法六(無視代理(lǐ))
function GetLocalIPAddress()
{
    var obj = null;
    var rslt = "";
    try
    {
        obj = new ActiveXObject("rcbdyctl.Setting");
        rslt = obj.GetIPAddress;
        obj = null;
    }
    catch(e)
    {
        //
    }
    
    return rslt;
   }

來(lái)自印度的(de)MCT Maulik Patel提供了(le)一種服務端的(de)解決方案,很好:

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
     ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
     ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}

備注:
1. 有些代理(lǐ)是不會發給我們真實IP地址的(de)
2. 有些客戶端會因爲“header_access deny”的(de)安全設置而不發給我們IP
 

net的(de)也(yě)可(kě)以參考http://www.cdnunion.com/articleinfo.php?pid=158設置

 

成都網站建設資料請點擊【成都網站建設公司介紹

成都網站設計案例請點擊【成都網站設計案例

馳創數碼
緻力打造互動品牌,提供精準營銷服務

立即咨詢

網站建設客服微信

掃一掃,添加微信咨詢

免費咨詢

400-602-8610

  • 地址:成都市高(gāo)新區(qū)錦城(chéng)大(dà)道666号奧克斯廣場(chǎng)B座902
  • 其他(tā)咨詢熱(rè)線:028-86666423 028-8666413
  • 郵箱:service@chichuang.com
  • 傳真:028-86666493