【问题标题】:how to get ip from domain in php?如何在php中从域中获取ip?
【发布时间】:2016-08-08 14:33:36
【问题描述】:

(我在 DOS 中执行我的代码)这是我的代码:

<?php
$handle = fopen("domann.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $ip = gethostbyname($line);
        echo "reading: ".$line. " ip: ".$ip  ;
    }

    fclose($handle);
} else {
    // error opening the file.
}

?>

我想要这样的东西:

阅读:www.abc.it ip:211.195.239.122

问题不在于 gethostbyname() 函数!! 它必须是 fgets($handle) 中的某些内容,它还为读取的每一行返回一个 \n

解决方案在这里:

$handle = fopen("file.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {

      $line=str_replace("\r\n","",$line);
        $ip = gethostbyname($line);

      echo "reading: ".$line." ip: ".$ip;
      echo "\n";
    }

    fclose($handle);
} else {
    echo "connection error";
}

【问题讨论】:

  • 您分享的代码有什么问题?错误?
  • 应该可以。也许在$line 上做一个var_dump 看看它是什么
  • 和domann.txt的内容和格式未知。
  • 我显然又一次置若罔闻。哦,不只是我。你是走路去学校还是怎么的?

标签: php ip ping


【解决方案1】:

嘿@Luca,我找到了为什么您的代码无法正常工作,这是因为在您的 domann.txt 中,您可能已经将网站命名为 http://google.com 或 http://www.google.com 不起作用,这是在 www.google.com 中起作用的唯一方法,因此在您的 domann.txt 中将您的网站网址命名为 www。后缀,仅此而已,这应该可以解决它。求证明

【讨论】:

  • 你可能有一些东西。我问了 OP 10 分钟他们的文本文件的内容是什么,没有回应。 As per this comment....
  • 是的,这是一种不好的做法,人们提出问题而不是寻找答案我同意@Fred-ii-,我也在等他
  • 给我一点时间!该文件包含一个域列表,例如 www.google.com,每行一个。
  • 如果我写 gethostbyname("www.google.it");它工作正常,但如果我写 gethostbyname($line);就像在我的示例中,我得到的输出是这样的:阅读:www.google.it ip:www.google.it
  • 在将它发送到 gethostbyname 之前尝试回显你的 $line 并查看它显示的内容,然后让我们看看
【解决方案2】:

试试这个,

http://php.net/gethostbyname

$names = file('domann.txt');
 foreach ($names as $name) {
     $ip = gethostbyname($name);
     echo  $ip.' <br>';
 }

【讨论】:

    【解决方案3】:

    解决方案在这里:

    $handle = fopen("file.txt", "r");
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
    
          $line=str_replace("\r\n","",$line);
            $ip = gethostbyname($line);
    
          echo "reading: ".$line." ip: ".$ip;
          echo "\n";
        }
    
        fclose($handle);
    } else {
        echo "connection error";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2013-04-27
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多