+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: IP Location with PHP

  1. #1
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    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:

    [highlight="SQL"]
    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
    [/highlight]

    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.

    Regards,
    Jaan
    Last edited by Jaan; 06-21-2009 at 07:15 AM.
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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!

  3. #3
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    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
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  4. #4
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,223
    Blog Entries
    8

    Re: IP Location with PHP

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

    What kind of program are you making?

  5. #5
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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.

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,648
    Blog Entries
    57

    Re: IP Location with PHP

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

  7. #7
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    Re: IP Location with PHP

    Oh thanks mate
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  8. #8
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,022
    Blog Entries
    1

    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!
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  9. #9
    Newbie azoqup is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    2

    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.

  10. #10
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    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
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. PHP 5 and OOP
    By Jordan in forum PHP Tutorials
    Replies: 11
    Last Post: 09-22-2008, 01:58 AM
  2. Beginners Guide To PHP: *Tutorial*
    By renlok in forum PHP Tutorials
    Replies: 3
    Last Post: 04-22-2008, 03:20 PM
  3. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 09:55 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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