【问题标题】:Bad Characters with inet_pton() PHP Function *Not Zend* Piwik location_ip issue带有 inet_pton() PHP 函数的错误字符 *不是 Zend* Piwik location_ip 问题
【发布时间】:2011-09-07 15:22:31
【问题描述】:

我正在尝试与我们安装在服务器上的 Piwik 数据库进行交互。在 Piwik 数据库中,存储的 IP 地址的值通过 inet_ntop() 插入。我正在尝试解码这些值并使用 inet_pton() 将它们提取出来,以便我可以运行查询以在存储在我们本地数据库中的 Piwik 数据库中查找客户 IP 地址。

问题出现在我构建查询时。我将存储在数据库中的地址通过 inet_pton() 运行,就像这样......

$data = mysql_fetch_assoc(mysql_query("SELECT ip_address FROM data_table WHERE id = 1"));

$more_data = mysql_fetch_assoc(mysql_query("SELECT location_ip FROM piwik_log_visit WHERE location_ip = '".inet_pton($data['ip_address'])."'"));

问题出在 inet_pton($data['ip_address']) 将显示随机字符(有时)包括黑色菱形问号。它返回一个 mysql_error,表示查询无效(因为字符错误)。我尝试添加 mysql_set_charset("utf8");在没有(好)结果运行查询之前。

有什么想法吗?

谢谢!

【问题讨论】:

  • 我和另一位朋友已经确定 inet_pton() 函数肯定有问题.....我们不知道为什么它会以这种方式转换字符。
  • 要从 Piwik DB 中选择原始数据,建议使用 REST API 来选择日志,因为它会为您完成这项工作,对大多数情况非常有用:) piwik.org/docs/analytics-api/reference/#Live

标签: php mysql character-encoding matomo


【解决方案1】:
$data = mysql_fetch_assoc(mysql_query("SELECT ip_address FROM data_table WHERE id = 1"));
$hexip = bin2hex(inet_pton($data['ip_address']));
$more_data = mysql_fetch_assoc(mysql_query("SELECT location_ip FROM piwik_log_visit WHERE hex(location_ip) = '$hexip'"));

为了让mysql和php不报错,需要将inet_pton的二进制输出转换为hex,然后将mysql存储的hex(location_ip)值与$hexip变量进行比较。

【讨论】:

    【解决方案2】:

    二元故事:

    $bin_ip = '0x'.bin2hex(inet_pton($data['ip_address']));
    
    "SELECT location_ip FROM piwik_log_visit WHERE hex(location_ip) = $bin_ip"
    

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2018-08-20
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多