Jump to content

GeoIP Help (Kinda)


DomenicF

Recommended Posts

Hey guys,

 

My host doesn't support the native GeoIP functions of PHP, so they suggest to use the Perl module that they have installed on the OS to do the work. Here is my code:

 

$country = shell_exec('geoip-lookup '.$_SERVER['REMOTE_ADDR']);

if($country == "US")
{
	$temp = $temp_F;
	echo "Fahrenheit";
}
elseif($country != "US")
{
	$temp = $temp_C;
	echo "Celcius";
}
else {
	echo "This is a recording.";
}

 

Now, even when $country is definitely set to US, it still always comes back to Celcius. I've already tried doing the script without the elseif, and it doesn't make a difference. Looking for some help.

Link to comment
https://forums.phpfreaks.com/topic/268379-geoip-help-kinda/
Share on other sites

You've echoed $country inside these IF blocks and it echoes "USCelcius"?

 

Yes, it echoes Celcius and when I echo $country it echoes US.

 

Can we see the code for the geoip-lookup program?

 

I don't have it, but I believe it is a perl module. Here's my host's wiki: http://wiki.dreamhost.com/GeoIP

 

Here is an example output of geoip-lookup:

 

$ geoip-lookup google.com
US

Link to comment
https://forums.phpfreaks.com/topic/268379-geoip-help-kinda/#findComment-1377971
Share on other sites

Hi.

 

Did you tried var_dump() ?

I just tried it, and you may be on to something. The output of var_dump is: string(3) "US "

 

So, I changed my code to:

 

$country = shell_exec('geoip-lookup '.$_SERVER['REMOTE_ADDR']);

if($country == "US ")
{
	$temp = $temp_F;
	echo "Fahrenheit<br />";
	echo $country."<br />";
	var_dump($country);
}
elseif($country != "US ")
{
	$temp = $temp_C;
	echo "Celcius<br />";
	echo $country."<br />";
	var_dump($country);
}
else {
	echo "This is a recording.<br />";
	var_dump($country);
}

 

And I still have the same results. With that being said, I did:

 

trim($country);

 

And it still outputs as 3 characters. So, I'm not sure what's going on.

Link to comment
https://forums.phpfreaks.com/topic/268379-geoip-help-kinda/#findComment-1377982
Share on other sites

So in the end I just assigned trim() to another variable and got it to work. My code now is as follows:

 

$country = shell_exec('geoip-lookup '.$_SERVER['REMOTE_ADDR']);
$output_country = trim($country);

if($output_country == "US")
{
	$temp = $temp_F;
	echo "Fahrenheit<br />";
	echo $country."<br />";
	var_dump($country);
}
elseif($output_country != "US")
{
	$temp = $temp_C;
	echo "Celcius<br />";
	echo $country."<br />";
	var_dump($country);
}
else {
	echo "This is a recording.<br />";
	var_dump($country);
}

 

Appreciate your time!

Link to comment
https://forums.phpfreaks.com/topic/268379-geoip-help-kinda/#findComment-1377987
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.