Home > Programming, Servers & Scripts >

Geoip code on Beyondhosting (11)


09-25-2013 10:17 AM #1 suipowers (Member)
Geoip code on Beyondhosting

I use the Geoip code on Beyondhosting:

Code:
<?php
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] ); //This will pull information for the client ip.
echo "<h1>How's the weather in ".$geo['city'].", ".$geo['region']."</h1>"; //This will display "How's the weather in Cincinnati, Ohio"
?>
I want to know if it doesn't find a city name, and so displays a blank.
In that case, how can i display something else - like "Your City"?

Thank you!


09-25-2013 10:33 AM #2 caurmen (Administrator)

This code should do what you want:

Code:
<?php
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] ); //This will pull information for the client ip.
if ($geo == "") {
$geo = "Your City";
}
echo "<h1>How's the weather in ".$geo['city'].", ".$geo['region']."</h1>"; //This will display "How's the weather in Cincinnati, Ohio"
?>
Let me know if you have any problems with it!


09-25-2013 11:59 AM #3 suipowers (Member)

Thank you, i just test that, it still can't display "Your City", don't know what's wrong?


09-25-2013 12:06 PM #4 caurmen (Administrator)

Hmm - what does it display?


09-25-2013 12:25 PM #5 suipowers (Member)

I used proxy to test, it display a blank.


09-25-2013 01:11 PM #6 dragoshsd (Member)

Proxies are detected weirdly sometimes. What does it display when you try with your own IP? The code above has worked fine for me!


09-25-2013 01:56 PM #7 suipowers (Member)

yes, it is ok when i don't use proxy, but if it doesn't find a city name, still displays a blank.


09-25-2013 06:16 PM #8 JasperP (Member)

I think Caurmen's code needs to be adjusted slightly:

Code:
<?php
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] ); //This will pull information for the client ip.

if ($geo['city'] == "")
   $geo['city'] = "Your City";
if ($geo['region'] == "")
   $geo['region'] = "Your Region";

echo "<h1>How's the weather in ".$geo['city'].", ".$geo['region']."</h1>"; //This will display "How's the weather in Cincinnati, Ohio"
?>
OR try this:

Code:
<?php
$geo = geoip_record_by_name( $_SERVER['REMOTE_ADDR'] ); //This will pull information for the client ip.

if ($geo == "") {
   $geo['city'] = "Your City";
   $geo['region'] = "Your Region";
}   

echo "<h1>How's the weather in ".$geo['city'].", ".$geo['region']."</h1>"; //This will display "How's the weather in Cincinnati, Ohio"
?>
Let us know if this doesn't work.


09-26-2013 02:38 AM #9 suipowers (Member)

Hi JasperP, cool, thank you very much! I have test these, the first is ok now!


09-27-2013 10:45 AM #10 caurmen (Administrator)

Nice one, Jasper, you're absolutely right. Thanks for the fix!

My guess is variation #1 will work better in general - the MaxMind library will often populate some fields of the array but leave other fields blank. The City field is especially prone to coming back with nothing.


10-04-2013 04:13 PM #11 BeyondHosting-Tyler (Member)

Thanks for the sample code!


Home > Programming, Servers & Scripts >