ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DataMapper - Using INET_NTOA/INET_ATON?

February 15, 2011 11:45am

Subscribe [2]
  • #1 / Feb 15, 2011 11:45am

    sorenchr

    30 posts

    I recently started using DataMapper, great tool. I’m trying to use MySQL’s INET_ATON to store a users IP (in a numerical value), and then retrieve the IP using INET_NTOA, however I don’t know if DataMapper even supports these the use of these functions. Anyone with some DMZ experience who knows if this can be done?

    MySQL documentation on INET_NTOA/INET_ATON:
    http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton

    Thanks for your time!

  • #2 / Feb 15, 2011 5:05pm

    Basketcasesoftware

    442 posts

    DataMapper allows direct access to ALL of MySQL’s functions. Let see if I can find the part in the manual again.

  • #3 / Feb 15, 2011 5:59pm

    Basketcasesoftware

    442 posts

    use the func method on your data object. It allows you to call any mySQL function, built in or user. Look under SQL Functions.

  • #4 / Feb 15, 2011 9:04pm

    sorenchr

    30 posts

    Do you know how to set it up if I want to save to the db using INET_ATON? Something like:
    I’ve set the users ip to:

    $user->last_ip = $_SERVER['REMOTE_ADDR'];

    But adding something like:

    $user->func('INET_ATON', '@last_ip');

    doesn’t work. Any ideas?

  • #5 / Feb 15, 2011 9:37pm

    Basketcasesoftware

    442 posts

    Do you know how to set it up if I want to save to the db using INET_ATON? Something like:
    I’ve set the users ip to:

    $user->last_ip = $_SERVER['REMOTE_ADDR'];

    But adding something like:

    $user->func('INET_ATON', '@last_ip');

    doesn’t work. Any ideas?

    I admit I’m not familiar with the ‘@varible’ argument type. Actually I’ve been exposed to it but don’t remember details.

    What kind of arguments is INET_ATON requiring?

  • #6 / Feb 15, 2011 9:41pm

    KarlBallard

    45 posts

    INET_ATON()
    - Return the numeric value of an IP address

    INET_NTOA()
    - Return the IP address from a numeric value

  • #7 / Feb 15, 2011 11:10pm

    Basketcasesoftware

    442 posts

    Ok, that’s what it does, but under the hood what is an ‘IP address’ to mySQL? 4 bytes?
    I mean, all he might require is

    $user->last_ip = $_SERVER['REMOTE_ADDR'];  
    $user->func('INET_ATON', $user->last_ip);

    It depends a lot on what $_SERVER[‘REMOTE_ADDR’] is.

  • #8 / Feb 16, 2011 12:09pm

    sorenchr

    30 posts

    Ok, that’s what it does, but under the hood what is an ‘IP address’ to mySQL? 4 bytes?
    I mean, all he might require is

    $user->last_ip = $_SERVER['REMOTE_ADDR'];  
    $user->func('INET_ATON', $user->last_ip);

    It depends a lot on what $_SERVER[‘REMOTE_ADDR’] is.

    $_SERVER[‘REMOTE_ADDR’] contains the visitors IP-address in it’s normal form, for example: 175.142.012.121.

    I tried the sample you provided, it didn’t work. I think the DMZ documentation lacks some clarity on this part, I absolutely cannot figure out how to do this.

  • #9 / Feb 16, 2011 3:53pm

    Basketcasesoftware

    442 posts

    You’re just trying to record an IP address, right? CodeIgniter has some helper functions for that. All you have to do is record whatever they spit out in your database. You don’t need any special function for that as far as I know.

  • #10 / Feb 16, 2011 5:29pm

    sorenchr

    30 posts

    Well, I want to use the MySQL functions because they are easy and allows you to store an IP-address as an integer. Surely there must be someone on this forum who knows how to use DMZ’s func methods.

  • #11 / Feb 16, 2011 5:32pm

    Basketcasesoftware

    442 posts

    wanwizard is the prime candidate for that one.

    And I can write a function to convert an IP address to an integer and back again quite easily. There are lots of ways to skin this cat.

  • #12 / Feb 16, 2011 5:52pm

    KarlBallard

    45 posts

    I’ve done some research and it turns out that you don’t need to do it that way..

    $ip_address = $_SERVER['REMOTE_ADDR'];
    $inet_aton = ip2long($_SERVER['REMOTE_ADDR']);
    
    $data = array(
        'ip_address' => $ip_address,
        'ip2long' => ip2long($ip_address),
        'long2ip' => long2ip($inet_aton),
    );
    
    echo '<pre>';
    print_r($data);
    echo '</pre><p>‘;
    </pre>


    Output on a localhost:

    Array
    (
        [ip_address] => 127.0.0.1
        [ip2long] => 2130706433
        [long2ip] => 127.0.0.1
    )

    Google is your friend, use it.

  • #13 / Feb 16, 2011 6:06pm

    sorenchr

    30 posts

    Thanks Karl! But that whole deal compared to a single line of code isn’t really worth it.

  • #14 / Feb 16, 2011 6:09pm

    KarlBallard

    45 posts

    The bits you require are will be one-liners.. I just put them into an example and dumped the results..

    You need to focus on the ip2long and the long2ip functions…

  • #15 / Feb 17, 2011 10:29am

    sorenchr

    30 posts

    The bits you require are will be one-liners.. I just put them into an example and dumped the results..

    You need to focus on the ip2long and the long2ip functions…

    You are absolutely right, I think I raced through your answer. That’ll certainly work and I think I’ll use it 😊, thanks again. It would be nice though to find a solution to my initial question.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases