Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: IP Location with PHP

  1. #1
    Jaan Guest

    IP Location with PHP

    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

    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 trimstr_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.



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

    checkip.php

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

    ?>
    And it's done.. I hope it helped and you liked it.
    Last edited by Roger; 01-04-2011 at 08:14 PM. Reason: revert tutorial

  2. CODECALL Circuit advertisement

     
  3. #2
    Jordan Guest

    Re: IP Location with PHP

    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!

  4. #3
    Jaan Guest

    Re: IP Location with PHP

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

    thanks mate

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: IP Location with PHP

    Yes I could see this being helpful with stats and ad management.

    What kind of program are you making?

  6. #5
    Jordan Guest

    Re: IP Location with PHP

    You can also use the PEAR package to determine location: Net_GeoIP OR Net_Geo

    I just came across these in the PEAR db.

  7. #6
    Join Date
    Jul 2006
    Posts
    16,475
    Blog Entries
    75
    Rep Power
    143

    Re: IP Location with PHP

    Cool! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Jaan Guest

    Re: IP Location with PHP

    Oh thanks mate

  9. #8
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: IP Location with PHP

    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!

  10. #9
    azoqup is offline Newbie
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Re: IP Location with PHP

    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.

  11. #10
    Jaan Guest

    Re: IP Location with PHP

    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

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find Form's Location
    By DenKain in forum C# Programming
    Replies: 3
    Last Post: 10-04-2011, 11:31 AM
  2. Java setting location
    By vaironl in forum Java Help
    Replies: 6
    Last Post: 10-02-2011, 11:43 AM
  3. General Offset and Base location
    By AKMafia001 in forum Assembly
    Replies: 2
    Last Post: 09-26-2011, 07:00 PM
  4. Geo-Location Programming
    By wilkinsonsm4 in forum General Programming
    Replies: 4
    Last Post: 01-14-2011, 09:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts