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
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();
).