php - 使用 PHP 检查 IPv4/IPv6 地址是否已启动

标签 php linux ping ipv6 ipv4

我有一个 IPv4 地址。前任。 172.19.20.21

我曾经这样做过

$fs = @fsockopen($ip,$port,$errno,$errstr,3);
if( !$fs ){
  $error = 'SSC is down';
  return Redirect::to('/')->with('error', $error )
  ->withInput(Request::except('password'));
}

它工作得很好。

现在,我有一个 IPv6 地址,例如。 3000::1

if ((strpos($ip, ":") > -1)){

     // Code for IPv6 check
    $fs = @fsockopen($ip,$port,$errno,$errstr,3);
    if( !$fs ){
      $error = 'SSC is down';
      return Redirect::to('/')->with('error', $error )
      ->withInput(Request::except('password'));
    }

}else{

    // Code for IPv4 check
    $fs = @fsockopen($ip,$port,$errno,$errstr,3);
    if( !$fs ){
      $error = 'SSC is down';
      return Redirect::to('/')->with('error', $error )
      ->withInput(Request::except('password'));
    }
}

我可以使用上面的代码吗?或者我需要寻找其他 IPv6 解决方案?

最佳答案

解析 IP 地址的内置方法是 inet_pton() :

This function converts a human readable IPv4 or IPv6 address (if PHP was built with IPv6 support enabled) into an address family appropriate 32bit or 128bit binary structure.

要确定格式,您可以计算结果字节数:

strlen(inet_pton($ip))
4 bytes = 32 bits = IPv4
16 bytes = 128 bits = IPv6

(不过,在您的具体用例中,不清楚为什么您首先需要这个。)

关于php - 使用 PHP 检查 IPv4/IPv6 地址是否已启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298670/

相关文章:

php - 使用 for 循环加速 PHP 代码输入

linux - 难以理解一个简单的 shell 脚本

c - 如何使用另一个源文件中定义的结构?

c - 如何使用 C 套接字 ping

c# - 异步任务中出现大量 ping - 出现异常 "An asynchronous call is already in progress."

loops - Ping 主机名列表并将结果输出到 powershell 中的 csv

php - 如何区分页面刷新和关闭页面

PHP 在列中显示图像

php - 使用 PHP 将 JSON 解析为 MySQL

linux - 为什么 Linux diff 实用程序在内容相同时产生输出?