跳到主要內容

取得使用者的IP位址 - PHP

原本使用$_SERVER['REMOTE_ADDR'],卻遇到使用者在Proxy Server底下,這只會取得Proxy Server的IP,既然如此只好換個方式了。


<?php 
$userIP = null; 
if(!empty($_SERVER['HTTP_CLIENT_IP'])){ 
    $userIP = $_SERVER['HTTP_CLIENT_IP']; 
}else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ 
    $userIP = $_SERVER['HTTP_X_FORWARDED_FOR']; 
}else{ 
    $userIP = $_SERVER['REMOTE_ADDR']; 
}  
echo $userIP; 
?>

提供給大家當作參考囉!

留言

這個網誌中的熱門文章

What is phpize

What is phpize According to the PHP official document : The phpize command is used to prepare the build environment for a PHP extension. If you need to build such an extension that from github or another code repositories, you can use  build tools to perform the build manually.