1

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);
4

2 回答 2

2

您确定 IP 是使用存储的inet_pton吗?

如果我们简单地将每 2 个字符转换为十进制,结果如下:

<?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"
?>

演示

那是你正确的IP吗?

如果 inet_pton(这没有意义,因为其中大多数是常规 ASCII 范围之外的字符),那么您可以简单地使用inet_ntop它来取回它。

要将 IP 转换为这种十六进制格式,您可以执行以下操作:

<?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
?>

演示

于 2013-08-20T08:40:32.180 回答
0

因此,查找该功能是一个好主意:

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

你会看到 inet-nton() 对其进行解码 - 试试看你会得到什么

于 2013-08-20T08:29:07.927 回答