Take a look at:
The IP-to-Country Handbook
You can download this free database
Once you have imported the database, you need to run this query to get country from the IP:
$country_query = "SELECT country_code2,country_name FROM iptoc
WHERE IP_FROM<='$REMOTE_ADDR' AND IP_TO>='$REMOTE_ADDR'";