Check if array is empty or not.
I am developing analytics site for my main site. I need to get the IP address of the user visiting. So I googled and i got a solution. But the solution is not working. The code is :-
PHP Code:
function getUserIpAddr(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
return $_SERVER['HTTP_CLIENT_IP'];
}else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
return $_SERVER['REMOTE_ADDR'];
}
}
mysql_query("INSERT INTO viewers (id,ip,p) VALUES('$uid','".getUserIpAddr()."','".$_POST['page']."')");
This works fine in localhost server, but it isn't working on Internet Server. Can someone please help me.
I think the problem is with empty function.
Will doing this function returns the ip address of user or the server ?
I created this function to get the IP address of client.
PHP Code:
function getIP(){
$response=@file_get_contents('http://www.netip.de');
if (empty($response)){throw new InvalidArgumentException("Error contacting Geo-IP-Server");}
$patterns=array();
$patterns["IP"] = '#IP: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["city"] = '#City: (.*?)<br#i';
foreach ($patterns as $key => $pattern){
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
return $ipInfo["IP"];
}
I have a doubt. If I run this code on my server. Will the script returns the IP address of client or the server ?