SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Storing IP Address and Hostname

    I would like to store a User's IP Address and Hostname.

    What Data-Type and Field-Size should I be using?

    I am using the following PHP functions...
    PHP Code:
        // Capture User's IP Address.
        
    $userIPAddress $_SERVER['REMOTE_ADDR'];
                
        
    // Capture User's Host Name.
        
    $userHostname gethostbyaddr($_SERVER['REMOTE_ADDR']); 
    Thanks,


    Debbie

  2. #2
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IP adddresses are in IPv4 or IPv6 format. Do a quick google to find the max length of each string and set your field size accordingly.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lyndah View Post
    IP adddresses are in IPv4 or IPv6 format. Do a quick google to find the max length of each string and set your field size accordingly.
    This is a MUCH more complex topic than you give it credit.


    Debbie

  4. #4
    Non-Member
    Join Date
    Jan 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    This is a MUCH more complex topic than you give it credit.
    I don't see why it should be. I've always found it straight forward. The only thing I can't remember off the top of my head is the IPv6 format so that is why I sggested you google it.

  5. #5
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the php functions you are using to get the data have NO RELEVANCE on what data type and length you choose to store the data in the database.

    There is nothing complicated about it. Figure out the length of the IP6 and use that, guess the longest length of your hostname and double that and there you go.

    Sorry not complicated at all.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    As an aside, I wouldn't store the hostname of each and every visitor at all.

    1) Doing a lookup of the hostname takes time, and we're not talking microseconds here, it can be quite costly if the route to the visitor is long. This means it will take longer for the site to load on their end, which is a bad thing.

    2) You don't really need them as hostnames rarely, if ever, change, so there is not really a need to store them. If at any point in time you want to know someone's host name you can just do a reverse lookup from their IP then and there.

    3) Some spammers have their reverse DNS set to "localhost". So if you do a reverse lookup and then ban the hostname you're effectively letting the server ban itself, so php can't connect to mysql anymore, you can't send or receive mail anymore, etc, etc. Obviously this is a Bad Idea (tm).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    As an aside, I wouldn't store the hostname of each and every visitor at all.

    1) Doing a lookup of the hostname takes time, and we're not talking microseconds here, it can be quite costly if the route to the visitor is long. This means it will take longer for the site to load on their end, which is a bad thing.
    Actually, while not microseconds we MAY be talking about milliseconds. A few months back when I was launching a site I decided to test it and set up a script that timed hostname lookups with php's gethostbyaddr() and logged the results in a file. Today I have 261380 timings over the period of 4 months. Here are the results:

    - 93.65% took less than 10ms (<0.0010s)
    - 99.45% took less than 0.1s
    - 0.489% (1278) took between 0.1s and 1s
    - 0.0658% (172) took more than 1s
    - the maximum was 10.5s

    I wouldn't find these timings troubling at all. However, I've heard that they can vary greatly depending on the server so it's best to test it first.

    I like getting the hostname for the purpose of storing it in a log file or log table in the db. It's very convenient to view the logs and have the hostname straight in the notepad or table view.

  8. #8
    SitePoint Addict bimalpoudel's Avatar
    Join Date
    Feb 2009
    Location
    Kathmandu, Nepal
    Posts
    279
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also love MySQL's conversions for the IP addresses to numbers and back to IPs.
    INET_ATON('192.168.1.1') Results: 3232235777
    INET_NTOA('3232235777') Results: 192.168.1.1

    Also, PHP has the ip2long(), and long2ip() functions.

    In the database, you can design INT or VARCHAR according to you needs.
    gethostbyaddr() always gives a string.

    Also, for the REMOTE_ADDR, try to find out a true IP first.

    And finally, don't miss a real solution - IP to Country that shows how to handle the ranges of IPs.
    Bimal Poudel @ Sanjaal Framework » over Smarty Template Engine
    ASKING INTERESTING QUESTIONS ON SITEPOINT FOURM

    Hire for coding support - PHP/MySQL

  9. #9
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by bimalpoudel View Post
    You might also love MySQL's conversions for the IP addresses to numbers and back to IPs.
    INET_ATON('192.168.1.1') Results: 3232235777
    INET_NTOA('3232235777') Results: 192.168.1.1
    Will those work with IPv6?

  10. #10
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    As an aside, I wouldn't store the hostname of each and every visitor at all.
    As mentioned above, normally it does not take that long time to get the hostname information.

    Quote Originally Posted by bimalpoudel View Post
    You might also love MySQL's conversions for the IP addresses to numbers and back to IPs.
    INET_ATON('192.168.1.1') Results: 3232235777
    INET_NTOA('3232235777') Results: 192.168.1.1

    Also, PHP has the ip2long(), and long2ip() functions.
    Keep in mind that ip2long returns an signed value, so you will need to convert it to unsigned.

    In addition please keep in mind that both the PHP and MySQL functions are for ipv4 only... So if your server is enabled to accept ipv6 requests, you need to make certain that you check what version the IP is first.

    If your writing a new application now, I would recommend using inet_ntop and inet_pton instead (PHP) since that support both ipv4 and ipv6. MySQL is also getting INET6_ATON() and INET6_NTOA() from ver.5.6.


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
  •