php - 从 PHP 将 IP 存储在 MySQL 中

标签 php mysql pdo

经过数小时的搜索,我只能找到“过时”和/或“不完整”的答案。 (显然它们早于 PDO。)我正在编写 PHP 代码(版本 7.0.15),并使用 MySQL 版本 5.7.17-0(均在 KUbuntu“虚拟机”上)。尽管我从事计算机工作已超过 45 年,但我对 PHP 和 MySQL 还是相当陌生。

我可以用 PHP 获取访问者的 IP 地址。然后我想检查“try_ur_table”以查看它是否已经有一个条目,如果没有,则插入一个条目并查找它,这样我就可以在程序的其他部分使用“ur_index”。 ur_index 是 int(11),ur_ip 是 binary(16)。

问题是每次我运行代码时,第一次选择失败,所以创建了一个新条目,然后第二次选择也找不到匹配项!

这是相关的代码片段:

  try
  {
    $pdc = new PDO('mysql:host=localhost;dbname=toy_database', 'xxxxx', 'xxxxx' );
    $pdc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdc->exec('SET NAMES "utf8"');
  }
  catch (PDOException $e)
  {
    $output = 'Unable to connect tothe databaseserver. ' . $e->getMessage();
    include 'errout.html.php';
    exit();
  }
  // Find the caller data...
  if ( isset( $_SERVER[ 'REMOTE_ADDR' ] ) )
  {
    $cd = inet_pton( $_SERVER[ 'REMOTE_ADDR' ] );
    if ( $cd )
    {
      // inet_pton thinks it succeeded...
      try
      {
        $sql = "SELECT * FROM try_ur_table WHERE ur_ip = ? ";
        $rt = $pdc->prepare($sql);
        $rt->execute(array($cd));
        $u_list = $rt->fetchAll();
      }
      catch (PDOException $e)
      {
        $output = 'Problem looking for ur_ip.  ' .  $e->getMessage();
        include 'errout.html.php';
        exit();
      }
      if ( $u_list == NULL )
      {
        // New user!
        try
        {
          $sqm = "INSERT INTO try_ur_table SET ur_ip=?";
          $rs = $pdc->prepare($sqm);
          $rs->execute(array($cd));
        }
        catch (PDOException $e)
        {
          $output = 'Problem inserting new ur_ip. ' . $e->getMessage();
          include 'errout.html.php';
          exit();
        }
        // Now go find the new entry...
        try
        {
          $sql = "SELECT * FROM try_ur_table WHERE ur_ip = ? ";
          $rt = $pdc->prepare($sql);
          $rt->execute(array($cd));
          $u_list = $rt->fetchAll();
        }
        catch (PDOException $e)
        {
          $output = 'Problem looking for new ur_ip.  ' . $e->getMessage();
          include 'errout.html.php';
          exit();
        }
      } // $u_list == NULL
      // At this point there should be exactly one row in $u_list...
    } // $cd != false
  }
  else
  {
    // ! isset( $_SERVER[ 'REMOTE_ADDR' ]
    $cd = false;
  }

其他测试表明 $_SERVER[ 'REMOTE_ADDR' ] 正在返回 127.0.0.1(这是有道理的,因为这是“本地主机”)。但是,每次我运行上面的代码时,phpMyAdmin 都会说 ur_ip 是 0x7f00000001000000000000000000000(希望我已经正确计算了零 - 似乎无法从 phpMyAdmin 复制和粘贴,但这是次要的)。另外,因为我有 ur_index,所以我尝试了基于它的选择,当我尝试通过 inet_ntop() 运行 ur_ip 时,我得到了垃圾,既不是有效的 IPv4 地址也不是 IPv6 地址。

最佳答案

您的问题源于二进制类型。要匹配存储值,您应该填充要比较的数据,如 the documentation 中所示。 .

或者您可以简单地使用 VARBINARY 来避免麻烦。

此外,您的代码非常重复,因此很难阅读和维护。异常的真正含义是您将它们全部捕获一次。还有一些条件是无用的,你不应该使用 fetchAll() 而应该使用 fetch method that is appropriate for your task .

当然,尝试解码不是实际的二进制值,但 phpmyadmin 显示的十六进制表示确实不会给您任何合理的结果。

以下是您要求的最小、完整且可验证的示例。它代表了实际问题,只要提供了正确的凭据,任何愿意运行它的人都可以使用。

try {
    $pdc = new PDO('mysql:host=localhost;dbname=toy_database;charset=utf8', 'xxxxx', 'xxxxx' );
    $pdc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // let's create a temporary table for the example purpose
    // change varbinary for binary and it goes astray
    $pdc->query("CREATE temporary TABLE try_ur_table 
                 (ur_index int primary key auto_increment,ur_ip varbinary(16))");

    $cd = inet_pton( $_SERVER["REMOTE_ADDR"] );

    $sql = "SELECT ur_index FROM try_ur_table WHERE ur_ip = ? ";
    $rt = $pdc->prepare($sql);
    $rt->execute(array($cd));
    $ur_index = $rt->fetchColumn();

    if ( !$ur_index )
    {
        $sqm = "INSERT INTO try_ur_table SET ur_ip=?";
        $pdc->prepare($sqm)->execute(array($cd));
        // here: that's all you need to get the index
        $ur_index = $pdc->lastInsertId();
    }

    // OK, let's verify
    $sql = "SELECT ur_ip FROM try_ur_table WHERE ur_ip = ? ";
    $rt = $pdc->prepare($sql);
    $rt->execute(array($cd));
    $ur_ip = $rt->fetchColumn();
    var_dump($ur_ip === $cd, bin2hex($ur_ip));

} catch (PDOException $e) {
    error_log($e);
    $output = ini_get('display_errors') ? $e : "There is a temporary problem. Try again later";
    include 'errout.html.php';
    exit();
}

关于php - 从 PHP 将 IP 存储在 MySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592818/

相关文章:

MySQL Innodb session 表 : Update query doesn't find a match

mysql - 在mysql中搜索多个表

php - 从静态方法访问非静态属性

php - 基于范围在mysql上搜索

php - 如何在页面中构建数据库调用?

php - 我无法在我的数据库中插入数据。我哪里错了?

php - fatal error : Exception thrown without a stack frame in Unknown on line 0, session_start

php - 当我使用 PDO PHP 查询 MySQL 数据库时出现令人困惑的错误

php - MySQL选择查询

php内存限制不能修改