Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

Convert IP Address To City and Country With PHP

<?php error_reporting (E_ALL ^ E_NOTICE); 
include('connect.inc'); 
session_start();
$db = public_db_connect();

public_db_connect();

echo 'Convert IP Address To City and Country With PHP<br/><br/>';
$command = "SELECT ip FROM ips ";
$result = mysqli_query($db, $command);
 
while($row = mysqli_fetch_assoc($result)){
 
$user_ip = $row['ip'];
 
$fetch_url =  'http://api.ipinfodb.com/v3/ip-city/?key=KEY_GOES_HERE&ip='.$user_ip.'&format=xml';
 
//CURL OPTION TO GET FILE
/*$mycurl = curl_init();
curl_setopt($mycurl, CURLOPT_URL,$fetch_url);
curl_setopt($mycurl, CURLOPT_FAILONERROR, true);
curl_setopt($mycurl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mycurl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($mycurl, CURLOPT_TIMEOUT, 30);
 
//fetch url and send it to the browser
$myfile= curl_exec($mycurl);*/

//FILE_GET_CONTENTS OPTION
$myfile = file_get_contents($fetch_url);
 
//create the new location object 
$location = new SimpleXMLElement($myfile);

// get the variables from the class
$country = $location->countryName;
$state = $location->regionName;
$city = $location->cityName;
 
$city_country_array[] = $city.",".$country;

}

// create an array with city and country 
$count = array_count_values($city_country_array);
print_r($count);
 
foreach($city_country_array as $country_city) {
 $all = explode(",",$country_city);
 $city = $all[0];
 $country = $all[1];

}
 echo '<br/>';
// loop to get the city, country and values of each entry
foreach($count as $key => $value) {
echo "<br/>".$key."-".$value;

}
?>