Question

I know we can use the user method of ion auth to get user data.

$user = $this->ion_auth->user()->row();
echo $user->ip_address;

But this gives an output u�8/ .How can I retrieve the actual ip address like 127.0.0.1.

UPDATE

Just to help others.

With help from h2ooooooo,I found that ipaddress is passed through bin2hex(inet_pton($ip)) and then saved in db. To retrieve it use

$user = $this->ion_auth->user()->row();
 echo inet_ntop($user->ip_address);
Was it helpful?

Solution

Are you sure that the IP is stored using inet_pton?

This is the result if we simply convert every 2 characters into decimal:

<?php
    $ipPacked = '75f1382f';

    $ipSegments = array();
    for ($i = 0; $i < 8; $i += 2) {
        $ipSegments[] = hexdec($ipPacked[$i] . $ipPacked[$i + 1]);
    }

    $ipReal = implode('.', $ipSegments);

    var_dump($ipReal); //string(13) "117.241.56.47"
?>

DEMO

Is that your correct IP?

If it is inet_pton (which doesn't make sense as most of those are characters outside regular ASCII scopes) then you can simply use inet_ntop to get it back.

To convert an IP into this hex format, you can do the following:

<?php
    $ip = '117.241.56.47';
    $ipSegments = explode('.', $ip);
    $ipPacked = '';
    for ($i = 0; $i < 4; $i++) {
        $ipPacked .= sprintf("%02s", dechex((int)$ipSegments[$i]));
    }
    echo $ipPacked; //75f1382f
?>

DEMO

OTHER TIPS

So, prolly a good idea to look that function up:

http://php.net/manual/en/function.inet-pton.php

You'll see that inet-nton() decodes it - give that a try & see what you get

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top