Welcome Guest, Not a member yet? Register   Sign In
DataMapper - Using INET_NTOA/INET_ATON?
#11

[eluser]Basketcasesoftware[/eluser]
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

[eluser]KarlBallard[/eluser]
I've done some research and it turns out that you don't need to do it that way..

Code:
$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>';


Output on a localhost:
Code:
Array
(
    [ip_address] => 127.0.0.1
    [ip2long] => 2130706433
    [long2ip] => 127.0.0.1
)

Google is your friend, use it.
#13

[eluser]sorenchr[/eluser]
Thanks Karl! But that whole deal compared to a single line of code isn't really worth it.
#14

[eluser]KarlBallard[/eluser]
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

[eluser]sorenchr[/eluser]
[quote author="KarlBallard" date="1297915795"]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...[/quote]

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

[eluser]KarlBallard[/eluser]
I'm not familiar with the Datamapper ORM, but surely something like:
Code:
$u->ip = ip2long($this->input->ip_address());
Would suffice?
#17

[eluser]Basketcasesoftware[/eluser]
Yep, it sure would. Smile

and
Code:
$ip_address=long2ip($u->ip);

for the reverse.
#18

[eluser]sorenchr[/eluser]
Shameless bumping. My initial question is yet to be answered Smile
#19

[eluser]WanWizard[/eluser]
The answer was already given:
Code:
// SELECT INET_NTOA(ipaddr) AS ip_address
$u->select_func('INET_NTOA', '@ipaddr', 'ip_address')->get();

As Datamapper objects are stored using the save() method, you can't use INET_ATON, you need to use one of the suggestions mentioned above and set $object->ipaddr.
#20

[eluser]sorenchr[/eluser]
[quote author="WanWizard" date="1298597481"]The answer was already given:
Code:
// SELECT INET_NTOA(ipaddr) AS ip_address
$u->select_func('INET_NTOA', '@ipaddr', 'ip_address')->get();

As Datamapper objects are stored using the save() method, you can't use INET_ATON, you need to use one of the suggestions mentioned above and set $object->ipaddr.[/quote]

You are quite right. Thanks for the response.




Theme © iAndrew 2016 - Forum software by © MyBB