Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.