tismon
tismon

Reputation: 511

Cannot access visitor's ip address in php

i am trying to store ip addresses of people who are visiting my web site. For that i use the below given code.

$serverIP=$_SERVER['REMOTE_ADDR'];

but instead of getting an IP like 112.200.xxx.xxx (when i visit), i got 192.9.200.195..

somebody please help me

thanks in advance

tismon

Upvotes: 2

Views: 414

Answers (4)

Nik
Nik

Reputation: 4075

You can also try this:

<?php
$var = file_get_contents('http://www.whatismyip.com/automation/n09230945.asp');
print $var;
?>

Upvotes: 0

oezi
oezi

Reputation: 51817

looks like you're thinking 192.9.200.195 is a local ip-adress - but its not, local adresses you mean are starting with 192.168.. 192.9.200.195 looks ok to me, if it's not, please try to explain you problem more detailed.

Upvotes: 2

Nik
Nik

Reputation: 4075

try

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
echo getRealIpAddr();

Upvotes: 1

Sebastian Br&#243;zda
Sebastian Br&#243;zda

Reputation: 879

try this, maybe its a proxy(?)

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
{
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
  $ip = $_SERVER['REMOTE_ADDR'];
} 
echo $ip;

Upvotes: 3

Related Questions