Heeey people!
I was thinking.. I haven't posted any tutorials lately so I was thinking I could make something useful today.. I hope you like this tutorial and enjoy
First of all you must download IPs database: Download
Now it's time to insert it into our database. But first you must make a database:
Code:CREATE TABLE `ip`.`locations` ( `from` VARCHAR( 100 ) NOT NULL , `to` VARCHAR( 100 ) NOT NULL , `short` VARCHAR( 10 ) NOT NULL , `cc3` VARCHAR( 100 ) NOT NULL , `cname` VARCHAR( 100 ) NOT NULL ) ENGINE = InnoDB
Now let's create an import.php file.
import.php
Now our ip database is inserted into our SQL database. Now it's time to make simple script that will get us our location.Code:<?php
// Connect to your database
$con = mysql_connect("localhost", "yourUsername", "yourPassword");
// Display an error if there was a problem with connection
if(!$con){
die(mysql_error());
}
// Select your database
$select_db = mysql_select_db("ip", $con);
// If there was problems with selecting your database.. It will display an error
if(!$select_db){
die(mysql_error());
}
// Select your database file
$db = file("ip-to-country.csv");
// Now let's get all rows separately and insert it into database
foreach($db as $row){
$content = trim( str_replace('"', "'", str_replace("'", "\'", $row) ) );
$sql = "INSERT INTO locations (from, to, short, cc3, cname) VALUES($content)";
$query = mysql_query($sql);
if(!$query){
die(mysql_error());
}
}
// If it's done.. Display this message
echo "Done!";
?>
If you want that cute flag in front of your country you must download those flags: Download
checkip.php
And it's done.. I hope it helped and you liked it.Code:<?php
// Let's start our function
function check_country($ip){
// Connect to database
$con = mysql_connect("localhost", "yourUsername", "yourPassword");
if(!$con){
die(mysql_error());
}
// Select database
$select_db = mysql_select_db("ip", $con);
if(!$select_db){
die(mysql_error());
}
// Now let's get the long ip
$real_ip = ip2long($ip);
$sql = "SELECT cname, short FROM locations WHERE from <= '$real_ip' and to >= '$real_ip'";
$sql = mysql_query($sql);
$cd = mysql_fetch_assoc($sql);
$country = ucwords(strtolower($cd['cname']));
$short = strtolower($cd['short']);
// Let's display your user's ip and location
echo "IP: $ip<br />";
echo "Location: <img src='flags/$short.gif' /> $country<br />";
}
check_country($_SERVER['REMOTE_ADDR']);
?>
Last edited by Roger; 01-04-2011 at 08:14 PM. Reason: revert tutorial
Very cool and useful tutorial! Nice work Jaan. This would be handy in many situations. If you wanted to restrict ads by location or allow (or block) a country, etc.
+rep!
yup yup mate.. I'm using this kind of technique in AdStar also.. but little bit.. well much complicated scriptbut yes it's useful.. then you know who visits your site.. and things
thanks mate![]()
Yes I could see this being helpful with stats and ad management.
What kind of program are you making?
Cool! +rep![]()
Oh thanks mate![]()
Very cool!This is great, I can use this in my project and avoid having to store the countries in my database.
+rep for you!![]()
Hello,
$select_db = mysql_select_db("ip", $con); ///////// import.php
$select_db = mysql_select_db("iploc"); ///// checkip.php
i just going to learn this script from your tutorial but i saw this ambiguous lines.
Is this a writing mistake or it meanings something else cause two different db. Anyway excuse me if i am wrong.
oh one thing why you use backtick operator in mysql query.
thank you.
Oh yes.. There was a mistake.. "iploc" is a database that is in my localhost.. I tested this script there.. and I forgot it there.. thanks for noticing![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks