-2

Possible Duplicate:
How do I find a user's IP address with PHP?
How can I get the user's IP address in PHP?

I make application web based. I want to show the IP address of the user/visitor who visit my web. I've used the script to find the IP address but it didn't work and no IP show. How to solve it?? do I miss something? This is my php script :

function get_ip()
{
    global $ip;
    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;
}

$ip = get_ip();

Please, I really need your help ! Thank you ...

Community
  • 1
  • 1
chumyee
  • 91
  • 1
  • 3
  • 12

1 Answers1

5

Your script does not display the result from the function call, only assigns it to the $ip variable. You'd want to add the following to display its value:

echo 'IP Address: ' . $ip;

Cheers.

Luke Pittman
  • 870
  • 4
  • 12
  • 1
    actually, just do echo get_ip(); at the end of your file. You can get rid of $ip = get_ip() all together unless you need it somewhere else. No use declaring a variable unless your going to use it in more than one place! – alecwhardy Jun 19 '12 at 04:55
  • @alecwhardy - Good point. Just thought I would put it this way so they could get some insight on how functions work and how variables and functions can interact. :) – Luke Pittman Jun 19 '12 at 04:57
  • @alecwhardy: Another use of a variable is to give a value a name. Works even when used once and can make sense. – hakre Jun 19 '12 at 05:15
  • it works inside a function, globally its not a good idea to have any variables if you can keep everything inside a class as a property. – alecwhardy Jun 19 '12 at 05:17