0 關注者

類別 yii\helpers\IpHelper

繼承yii\helpers\IpHelper » yii\helpers\BaseIpHelper
起始版本2.0.14
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/helpers/IpHelper.php

IpHelper 類別提供了一組與 IP 相關的靜態方法。

方法預期正確的 IP 位址。若要驗證 IP 位址,請使用 IpValidator

公開方法

隱藏繼承的方法

方法 描述 定義於
expandIPv6() 將 IPv6 位址擴展為完整表示法。 yii\helpers\BaseIpHelper
getIpVersion() 取得 IP 版本。不執行 IP 位址驗證。 yii\helpers\BaseIpHelper
inRange() 檢查 IP 位址或子網路 $subnet 是否包含在 $subnet 中。 yii\helpers\BaseIpHelper
ip2bin() 將 IP 位址轉換為位元表示。 yii\helpers\BaseIpHelper

常數

隱藏繼承的常數

常數 描述 定義於
IPV4 4 yii\helpers\BaseIpHelper
IPV4_ADDRESS_LENGTH 32 IPv4 位址的位元長度 yii\helpers\BaseIpHelper
IPV6 6 yii\helpers\BaseIpHelper
IPV6_ADDRESS_LENGTH 128 IPv6 位址的位元長度 yii\helpers\BaseIpHelper

方法詳細資訊

隱藏繼承的方法

expandIPv6() public static method

定義於: yii\helpers\BaseIpHelper::expandIPv6()

將 IPv6 位址擴展為完整表示法。

例如,2001:db8::1 將會展開為 2001:0db8:0000:0000:0000:0000:0000:0001

public static string expandIPv6 ( $ip )
$ip 字串

原始有效的 IPv6 位址

回傳 字串

展開後的 IPv6 位址

                public static function expandIPv6($ip)
{
    $hex = unpack('H*hex', inet_pton($ip));
    return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
}

            
getIpVersion() public static method

定義於: yii\helpers\BaseIpHelper::getIpVersion()

取得 IP 版本。不執行 IP 位址驗證。

public static integer getIpVersion ( $ip )
$ip 字串

有效的 IPv4 或 IPv6 位址。

回傳 integer

IPV4IPV6

                public static function getIpVersion($ip)
{
    return strpos($ip, ':') === false ? self::IPV4 : self::IPV6;
}

            
inRange() public static method

定義於: yii\helpers\BaseIpHelper::inRange()

檢查 IP 位址或子網路 $subnet 是否包含在 $subnet 中。

例如,以下程式碼檢查子網路 192.168.1.0/24 是否在子網路 192.168.0.0/22

IpHelper::inRange('192.168.1.0/24', '192.168.0.0/22'); // true

如果您需要檢查單個 IP 位址 192.168.1.21 是否在子網路 192.168.1.0/24 內,您可以使用以下任何範例

IpHelper::inRange('192.168.1.21', '192.168.1.0/24'); // true
IpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // true

另請參閱 https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

public static boolean inRange ( $subnet, $range )
$subnet 字串

有效的 IPv4 或 IPv6 位址或 CIDR 範圍,例如:10.0.0.0/82001:af::/64

$range 字串

有效的 IPv4 或 IPv6 CIDR 範圍,例如:10.0.0.0/82001:af::/64

回傳 boolean

$subnet 是否包含於 $range 內

throws yii\base\NotSupportedException

                public static function inRange($subnet, $range)
{
    list($ip, $mask) = array_pad(explode('/', $subnet), 2, null);
    list($net, $netMask) = array_pad(explode('/', $range), 2, null);
    $ipVersion = static::getIpVersion($ip);
    $netVersion = static::getIpVersion($net);
    if ($ipVersion !== $netVersion) {
        return false;
    }
    $maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH;
    $mask = isset($mask) ? $mask : $maxMask;
    $netMask = isset($netMask) ? $netMask : $maxMask;
    $binIp = static::ip2bin($ip);
    $binNet = static::ip2bin($net);
    return substr($binIp, 0, $netMask) === substr($binNet, 0, $netMask) && $mask >= $netMask;
}

            
ip2bin() public static method

定義於: yii\helpers\BaseIpHelper::ip2bin()

將 IP 位址轉換為位元表示。

public static string ip2bin ( $ip )
$ip 字串

有效的 IPv4 或 IPv6 位址

回傳 字串

位元字串

throws yii\base\NotSupportedException

                public static function ip2bin($ip)
{
    $ipBinary = null;
    if (static::getIpVersion($ip) === self::IPV4) {
        $ipBinary = pack('N', ip2long($ip));
    } elseif (@inet_pton('::1') === false) {
        throw new NotSupportedException('IPv6 is not supported by inet_pton()!');
    } else {
        $ipBinary = inet_pton($ip);
    }
    $result = '';
    for ($i = 0, $iMax = strlen($ipBinary); $i < $iMax; $i += 4) {
        $result .= str_pad(decbin(unpack('N', substr($ipBinary, $i, 4))[1]), 32, '0', STR_PAD_LEFT);
    }
    return $result;
}