php - Check if IP belongs to Belgium -
i need check users ip if it's belongs belgium country.
i found list i.e: http://www.nirsoft.net/countryip/be.html
but how can compare users ip these ips ?
the solution suggested required basic programming , database skills. since not mentioned requirements, explain using use php , mysql in codes.
download , load the ip2location lite db1 database mysql. can table definition page https://lite.ip2location.com/database-ip-country
add following codes in php pages required detection.
//configure mysql connection $host = 'localhost'; $user = 'root'; $password = 'your_password'; $database = 'ip2location_database'; $table_name = 'ip2location_db1';
//get visitor ip address $ip = $_server['remote_addr'];
//in case testing locally 127.0.0.1, //you can uncomment below line assign ip address //to 8.8.8.8 (or whatever) testing. //$ip = '8.8.8.8';
try{ //create , perform sql query using pdo $db = new pdo('mysql:host=' . $host . ';dbname=' . $database . ';charset=utf8', $user, $password); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception);
$st = $db->prepare('select *
' . $table_name . '
inet_aton(:ip) <= ip_to limit 1'); $st->execute(array(':ip'=>$ip));$row = $st->fetch(pdo::fetch_assoc);
//print out result var_dump($row); } catch(pdoexception $e) { echo $e->getmessage(); }
- you can review record output screen. use country_code variable country result.
Comments
Post a Comment