0

How should I call this function? I'm new to PHP. Here's my code... but I have an error

Notice: Undefined variable: ip in C:\xampp\htdocs\PHPTest\ip.php on line 19

    <?php
    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;
    }
    call_user_func('getRealIpAddr', '$ip');
    echo $ip;
    ?>

UPDATE

Strange reason, I'm using Windows 10, localhost, xampp and google Chrome this script doesn't provide me an ip address! That's why a corrected code was empty... Thought it was some kind of errors or something

Second UPDATE

If you're getting no ip like me, you may try this solution on httpd.conf

Community
  • 1
  • 1
DagicCross
  • 401
  • 3
  • 13
  • for update: read this: http://stackoverflow.com/questions/3699454/should-a-mamp-return-1-as-ip-on-localhost – devpro Jan 28 '16 at 15:51
  • are u getting this: ::1 ??? and want to get this 127.0.0.1 ?? for UPDATE – devpro Jan 28 '16 at 15:55
  • @devpro empty and just updated my httpd.conf to `Listen 127.0.0.1:80`, restarted still didn't work :o – DagicCross Jan 28 '16 at 15:56
  • 1
    this question will help u: http://stackoverflow.com/questions/10982277/serverremote-addr-not-giving-the-right-ip-address – devpro Jan 28 '16 at 15:57
  • More likely the issue is `call_user_func('getRealIpAddr', '$ip');` remove the single quotes around `'$ip'` Plus there is no parameter defined on that function to receive `$ip` anyway and it would need to be passed by reference the way you are doing it! – RiggsFolly Jan 28 '16 at 15:57
  • It's working now, thanks :) – DagicCross Jan 28 '16 at 16:09

3 Answers3

4

Your function returns the IP address, so assign a variable to the return value of the function, like so:

$ip = getRealIpAddr();

mikeb
  • 10,578
  • 7
  • 62
  • 120
2

Error is very clear $ip is not defined:

Modified Code:

<?
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;
}

$ip = getRealIpAddr(); // your function
echo $ip;
?>

If you want to use $ip variable that you defined inside the function than note that scope of the $ip is limited into the function.

You can not call this variable outside the function. for this you need to store it in a variable as like above mentioned example ($ip = getRealIpAddr();).

devpro
  • 16,184
  • 3
  • 27
  • 38
-1

You can't access to a variable declared inside a function, but with the return statement you can do :

echo getRealIpAddr();

Instead of :

call_user_func('getRealIpAddr', '$ip');
echo $ip;
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33