SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: ip to country

  1. #1
    011521 dbalsdon's Avatar
    Join Date
    Feb 2003
    Location
    North Of Scotland
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ip to country

    I'm trying to create an ip to country script, based on the code at http://www.ip-to-country.com. So far I have the folowing code:

    PHP Code:
    <?php
            $ip 
    $_SERVER['REMOTE_ADDR'];
            
    $ip_number sprintf("%u"ip2long($ip));

            @
    mysql_connect("localhost","username","password") or die(mysql_error());
            
            @
    mysql_select_db("ip2country") or die(mysql_error());
            
            
    $country_query  "SELECT country_code2, country_name FROM iptocountry " .
                                
    "WHERE IP_FROM<='".$ip_number."' " .
                                
    "AND IP_TO>='".$ip_number."'";
            
            
    $country_exec mysql_query($country_query);

            
    $ccode_array mysql_fetch_array($country_exec);

            
    $country_code $ccode_array['country_code2'];
            
            
    $country_name $ccode_array['country_name'];
            
            echo 
    $country_code $country_name;
            
            
    mysql_close();
            
    ?>
    For some reason, nothing seems to happen, and whenever i try echoing out the results of some of the variables, nothing is displayed.

    Can anyone see whats wrong with the code? I think it might be a problem with the SQL statement, but I can't see whats wrong with it.

    Thanks for any help you can give.
    Daniel Balsdon
    My Site

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try $country_exec = mysql_query($country_query) or die('Error: '.mysql_error());

    then you'll see if somethings wrong with the sql

  3. #3
    011521 dbalsdon's Avatar
    Join Date
    Feb 2003
    Location
    North Of Scotland
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing is being displayed at all. When I added or die('Error: '.mysql_error()) nothing displayed(i even tried echo $country_exec.

    Besides that coding i posted above, there was nothing else in the document, so i tried ading a bit of basic html.. just <html><body><p>test</p><//body></html> yet nothing displayed.

    I'm at a loss. I have no idea why nothing is being displayed, not even any errors.
    Daniel Balsdon
    My Site

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Florida
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never used <//body>, I always use </body>
    was I absent an important day?

  5. #5
    SitePoint Addict sinapra's Avatar
    Join Date
    Sep 2004
    Location
    secunderabad
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try once removing php code and just put html and run the php file and check the result and let us know the same. if you have html output, then next process is, print something in php <?print "hi everyone";?>, lets know this first...........
    Regards

    Its Great to work and Be Merry!!!!!!!!

  6. #6
    011521 dbalsdon's Avatar
    Join Date
    Feb 2003
    Location
    North Of Scotland
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never used <//body>, I always use </body>
    thanks for the FANTASTIC help just a typo i made while writing the post.


    ok. i've removed the php coding, the html coding worked fine, so i used print 'test';... worked fine..

    printed the IP out to the screen successfully, then managed to print the ip number and sql statement aswell. (the SQL statement is: SELECT country_code2, country_name FROM iptocountry WHERE ip_from<='1369209109' AND ip_to>='1369209109'

    when i try all the coding again, nothing happens.
    Daniel Balsdon
    My Site

  7. #7
    SitePoint Addict sinapra's Avatar
    Join Date
    Sep 2004
    Location
    secunderabad
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check for the connections then, is it connecting to DB,
    Regards

    Its Great to work and Be Merry!!!!!!!!

  8. #8
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works fine for me
    PHP Code:
    <?php
    $ip 
    $_SERVER['REMOTE_ADDR'];
    $ip_number sprintf("%u"ip2long($ip));

    mysql_connect('localhost','user','password') or die(mysql_error());
    mysql_select_db('ip2country') or die(mysql_error());

    $country_query  "SELECT country_code2, country_name FROM iptocountry
      WHERE IP_FROM<='"
    .$ip_number."'
      AND IP_TO>='"
    .$ip_number."'";
    $country_exec mysql_query($country_query) or die(mysql_error());
    if(
    mysql_num_rows($country_exec)){
      
    $ccode_array mysql_fetch_array($country_exec);
      
    $country_code $ccode_array['country_code2'];
      
    $country_name $ccode_array['country_name'];
      echo 
    $country_code.' '.$country_name;
    } else {
      echo 
    'IP Address not found';
    }
    mysql_close();

    ?>

  9. #9
    Fully Sweet Car noddy's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Western Australia
    Posts
    759
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
            echo $country_code $country_name
    change the above to this

    PHP Code:
            echo $country_code.' '.$country_name.'<br>'."\n"

  10. #10
    SitePoint Enthusiast DmS's Avatar
    Join Date
    Jan 2004
    Location
    Stockholm, Sweden
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've actually just implemented this based on the same db (client's code though, can't show it).

    did you try the example code given on their site before modifying it?
    they use the inet_aton functionality in mysql
    PHP Code:
    IP_FROM <= inet_aton('$REMOTE_ADDR'".
    "
    AND IP_TO >= inet_aton('$REMOTE_ADDR'
    I've used that and it worked.

    One note though.
    The way this db table is structured you really must work on the indexes & datatypes to make the queries fast enough for high traffic.

    Tips:
    use int(10) as datatypes for the ip-ranges
    use innodb instead of myisam
    set a reversed index on ip_from and ip_to "PRIMARY KEY (`ip_to`,`ip_from`)"

    These actions improved the performance from 0.2 sec for an ip towards the end of the table to overall times of 0.02 - 0.01 which is a big difference.
    /D
    { knowledge is what remains once you forget what you learned }
    Home: DmSProject Tutorials: GurusNetwork
    Committed at:
    OzoneAsylum + Blog

  11. #11
    SitePoint Addict sinapra's Avatar
    Join Date
    Sep 2004
    Location
    secunderabad
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    same here, code looks ok, i have concerns over connections and other back-end aspects................
    Regards

    Its Great to work and Be Merry!!!!!!!!


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
  •