Jump to content

IP Location with PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Guest_Jaan_*

Guest_Jaan_*
  • Guests
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:


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


<?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!";


?>


Now our ip database is inserted into our SQL database. Now it's time to make simple script that will get us our location. ^^

Posted Image

If you want that cute flag in front of your country you must download those flags: Download

checkip.php


<?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']);


?>


And it's done.. I hope it helped and you liked it.

Edited by Roger, 04 January 2011 - 08:14 PM.
revert tutorial


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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!

#3
Guest_Jaan_*

Guest_Jaan_*
  • Guests
yup yup mate.. I'm using this kind of technique in AdStar also.. but little bit.. well much complicated script :D but yes it's useful.. then you know who visits your site.. and things :D

thanks mate :)

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yes I could see this being helpful with stats and ad management.

What kind of program are you making?

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You can also use the PEAR package to determine location: Net_GeoIP OR Net_Geo

I just came across these in the PEAR db.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Cool! +rep :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Guest_Jaan_*

Guest_Jaan_*
  • Guests
Oh thanks mate ^^

#8
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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! :D

#9
azoqup

azoqup

    Newbie

  • Members
  • PipPip
  • 11 posts
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.

#10
Guest_Jaan_*

Guest_Jaan_*
  • Guests
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 ^^

#11
smiling

smiling

    Newbie

  • Members
  • Pip
  • 1 posts
Hello,

First of all, thank you for the useful script and data.
I am new to php programming and need some advice.

I found difficulties trying to put this script into use. No matter what IP I passed to the check_country function, I always got 'united states' as the answer.

And then I tried to modify the database structure. I changed the 'from' field and 'to' field, from varchar to biginteger.
This seems to solve my problem. Different IPs yield different answer from the check_country function.

But I'm still uncertain whether or not the answer is the correct one because one of the IP I tried to pass to the function gives no result (no country). And yet I know for sure that the IP is a valid internet IP.

Please advise:
1. Why did I need to change the 'from' field and 'to' field to biginteger to make the script run correctly?
2. Why did I get no result for an IP that I know for sure is a valid one?

Thank you

#12
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
The original poster is no longer with CodeCall. If you have any question regarding this posting, please start a new thread in the appropriate section of the forum (and reference this thread).

Thank you.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.