PHP 방문자 실제 사용하는 IP(Real IP) 구하기
페이지 정보
본문
<?php
// 방법1
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip1 = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip1 = $_SERVER['REMOTE_ADDR'];
}
// 방법2
$ip2 = $_SERVER['HTTP_CLIENT_IP']?$_SERVER['HTTP_CLIENT_IP']:($_SERVER['HTTP_X_FORWARDED_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR']);
// 방법3. isset을 사용하여 통지를 제거한 코드
$ip3 = isset($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:isset($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
// 방법4
function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
$ip4 = getUserIP();
echo "코드1 = ". $ip1."<br><br>";
echo "코드2 = ". $ip2."<br><br>";
echo "코드3 = ". $ip3."<br><br>";
echo "코드4 = ". $ip4."<br><br>";
?>
데모 : https://www.happyjung.com/demo/php/x-real_ip.php
참고자료
https://code.i-harness.com/ko-kr/q/2dd309
https://stackoverflow.com/questions/13646690/how-to-get-real-ip-from-visitor
// 방법1
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip1 = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip1 = $_SERVER['REMOTE_ADDR'];
}
// 방법2
$ip2 = $_SERVER['HTTP_CLIENT_IP']?$_SERVER['HTTP_CLIENT_IP']:($_SERVER['HTTP_X_FORWARDED_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR']);
// 방법3. isset을 사용하여 통지를 제거한 코드
$ip3 = isset($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:isset($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR'];
// 방법4
function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
$ip4 = getUserIP();
echo "코드1 = ". $ip1."<br><br>";
echo "코드2 = ". $ip2."<br><br>";
echo "코드3 = ". $ip3."<br><br>";
echo "코드4 = ". $ip4."<br><br>";
?>
데모 : https://www.happyjung.com/demo/php/x-real_ip.php
참고자료
https://code.i-harness.com/ko-kr/q/2dd309
https://stackoverflow.com/questions/13646690/how-to-get-real-ip-from-visitor
댓글목록
등록된 댓글이 없습니다.