Using an API from http://www.ipgp.net/ this snippet demonstrates getting a city from an IP address pass the IP address in a var called $ip and the API key in a var called $apikey.
If the session has not been created then a lookup is done and the city is added to a session.
//lookup ip address for city
if(!isset($_SESSION[\'lookedupCity\'])){
$ip = $_SERVER[\'REMOTE_ADDR\'];
$string = file_get_contents(\"http://www.ipgp.net/api/xml/$ip/$apikey\");
$xml = new SimpleXMLElement($string);
$_SESSION[\'lookedupCity\'] = (string)$xml->City;
}