# BaseIpHelper

> Class BaseIpHelper provides concrete implementation for yii\helpers\IpHelper

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [helpers](/docs/api/yii/helpers/index.md) › BaseIpHelper

| class | `yii\helpers\BaseIpHelper` |
| --- | --- |
| Since | 2.0.14 |
| Source | [`framework/helpers/BaseIpHelper.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/helpers/BaseIpHelper.php#L21) |

1 subclass:

- [`IpHelper`](/docs/api/yii/helpers/iphelper/index.md)

Class BaseIpHelper provides concrete implementation for [`IpHelper`](/docs/api/yii/helpers/iphelper/index.md)

Do not use BaseIpHelper, use [`IpHelper`](/docs/api/yii/helpers/iphelper/index.md) instead.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`expandIPv6()`](#method-expandIPv6) static | Expands an IPv6 address to it's full notation. | `BaseIpHelper` |
| [`getIpVersion()`](#method-getIpVersion) static | Gets the IP version. Does not perform IP address validation. | `BaseIpHelper` |
| [`inRange()`](#method-inRange) static | Checks whether IP address or subnet $subnet is contained by $subnet. | `BaseIpHelper` |
| [`ip2bin()`](#method-ip2bin) static | Converts IP address to bits representation. | `BaseIpHelper` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-IPV4"></span>`IPV4` | `4` |  | `BaseIpHelper` |
| <span id="const-IPV6"></span>`IPV6` | `6` |  | `BaseIpHelper` |
| <span id="const-IPV6_ADDRESS_LENGTH"></span>`IPV6_ADDRESS_LENGTH` | `128` | The length of IPv6 address in bits | `BaseIpHelper` |
| <span id="const-IPV4_ADDRESS_LENGTH"></span>`IPV4_ADDRESS_LENGTH` | `32` | The length of IPv4 address in bits | `BaseIpHelper` |

## Method details

### expandIPv6() <span id="method-expandIPv6"></span>

```php
public static function expandIPv6(string $ip): string
```

Expands an IPv6 address to it's full notation.

For example `2001:db8::1` will be expanded to `2001:0db8:0000:0000:0000:0000:0000:0001`

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ip` | `string` | The original valid IPv6 address |

**Returns**

`string`: The expanded IPv6 address

### getIpVersion() <span id="method-getIpVersion"></span>

```php
public static function getIpVersion(string $ip): int
```

Gets the IP version. Does not perform IP address validation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ip` | `string` | The valid IPv4 or IPv6 address. |

**Returns**

`int`: [`IPV4`](/docs/api/yii/helpers/baseiphelper/index.md#const-IPV4) or [`IPV6`](/docs/api/yii/helpers/baseiphelper/index.md#const-IPV6)

### inRange() <span id="method-inRange"></span>

```php
public static function inRange(string $subnet, string $range): bool
```

Checks whether IP address or subnet $subnet is contained by $subnet.

For example, the following code checks whether subnet `192.168.1.0/24` is in subnet `192.168.0.0/22`:

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

In case you need to check whether a single IP address `192.168.1.21` is in the subnet `192.168.1.0/24`,
you can use any of theses examples:

```
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
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$subnet` | `string` | The valid IPv4 or IPv6 address or CIDR range, e.g.: `10.0.0.0/8` or `2001:af::/64` |
| `$range` | `string` | The valid IPv4 or IPv6 CIDR range, e.g. `10.0.0.0/8` or `2001:af::/64` |

**Returns**

`bool`: Whether $subnet is contained by $range

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md)

**See also**

- <https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing>

### ip2bin() <span id="method-ip2bin"></span>

```php
public static function ip2bin(string $ip): string
```

Converts IP address to bits representation.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$ip` | `string` | The valid IPv4 or IPv6 address |

**Returns**

`string`: Bits as a string

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md)