I use the Geoip code on Beyondhosting:
<?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" ?>
This code should do what you want:
<?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"
?>
Thank you, i just test that, it still can't display "Your City", don't know what's wrong?
Hmm - what does it display?
I used proxy to test, it display a blank.
Proxies are detected weirdly sometimes. What does it display when you try with your own IP? The code above has worked fine for me!
yes, it is ok when i don't use proxy, but if it doesn't find a city name, still displays a blank.
I think Caurmen's code needs to be adjusted slightly:
<?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" ?>
<?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"
?>
Hi JasperP, cool, thank you very much! I have test these, the first is ok now!
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.
Thanks for the sample code!