# HostControl

> HostControl provides simple control over requested host name.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [filters](/docs/api/yii/filters/index.md) › HostControl

| class | `yii\filters\HostControl` |
| --- | --- |
| Extends | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) » [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.11 |
| Source | [`framework/filters/HostControl.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/filters/HostControl.php#L74) |

HostControl provides simple control over requested host name.

This filter provides protection against ['host header' attacks](https://www.acunetix.com/vulnerabilities/web/host-header-attack),
allowing action execution only for specified host names.

Application configuration example:

```
return [
    'as hostControl' => [
        'class' => 'yii\filters\HostControl',
        'allowedHosts' => [
            'example.com',
            '*.example.com',
        ],
    ],
    // ...
];
```

Controller configuration example:

```
use yii\web\Controller;
use yii\filters\HostControl;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'hostControl' => [
                'class' => HostControl::class,
                'allowedHosts' => [
                    'example.com',
                    '*.example.com',
                ],
            ],
        ];
    }

    // ...
}
```

> Note: the best way to restrict allowed host names is usage of the web server 'virtual hosts' configuration.
This filter should be used only if this configuration is not available or compromised.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$allowedHosts`](#property-allowedHosts) | `array\|`[`Closure`](https://www.php.net/manual/en/class.closure.php)`\|null` | List of host names, which are allowed. Default: `null`. | `HostControl` |
| [`$denyCallback`](#property-denyCallback) | `callable\|null` | A callback that will be called if the current host does not match [`$allowedHosts`](/docs/api/yii/filters/hostcontrol/index.md#property-allowedHosts). Default: `null`. | `HostControl` |
| <span id="property-except"></span>[`$except`](/docs/api/yii/base/actionfilter/index.md#property-except) | `array` | List of action IDs that this filter should not apply to. Default: `[]`. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| [`$fallbackHostInfo`](#property-fallbackHostInfo) | `string\|null` | Fallback host info (e.g. `https://www.yiiframework.com`) used when [Request::$hostInfo](/docs/api/yii/web/request/index.md#property-hostInfo) is invalid. Default: `''`. | `HostControl` |
| <span id="property-only"></span>[`$only`](/docs/api/yii/base/actionfilter/index.md#property-only) | `array` | List of action IDs that this filter should apply to. Default: `[]`. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="property-owner"></span>[`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) | `yii\base\T\|null` | The owner of this behavior Default: `null`. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-afterAction"></span>[`afterAction()`](/docs/api/yii/base/actionfilter/index.md#method-afterAction) | This method is invoked right after an action is executed. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="method-afterFilter"></span>[`afterFilter()`](/docs/api/yii/base/actionfilter/index.md#method-afterFilter) |  | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="method-attach"></span>[`attach()`](/docs/api/yii/base/actionfilter/index.md#method-attach) | Attaches the behavior object to the component. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| [`beforeAction()`](#method-beforeAction) | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | `HostControl` |
| <span id="method-beforeFilter"></span>[`beforeFilter()`](/docs/api/yii/base/actionfilter/index.md#method-beforeFilter) |  | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detach"></span>[`detach()`](/docs/api/yii/base/actionfilter/index.md#method-detach) | Detaches the behavior object from the component. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="method-events"></span>[`events()`](/docs/api/yii/base/behavior/index.md#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`denyAccess()`](#method-denyAccess) protected | Denies the access. | `HostControl` |
| <span id="method-getActionId"></span>[`getActionId()`](/docs/api/yii/base/actionfilter/index.md#method-getActionId) protected | Returns an action ID by converting [`Action::$uniqueId`](/docs/api/yii/base/action/index.md#property-uniqueId) into an ID relative to the module. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |
| <span id="method-isActive"></span>[`isActive()`](/docs/api/yii/base/actionfilter/index.md#method-isActive) protected | Returns a value indicating whether the filter is active for the given action. | [`yii\base\ActionFilter`](/docs/api/yii/base/actionfilter/index.md) |

## Property details

### $allowedHosts <span id="property-allowedHosts"></span>

```php
public array|Closure|null $allowedHosts = null
```

List of host names, which are allowed.

Each host can be specified as a wildcard pattern. For example:

```
[
    'example.com',
    '*.example.com',
]
```

This field can be specified as a PHP callback of following signature:

```
function (\yii\base\Action $action) {
    //return array of strings
}
```

where `$action` is the current [action](/docs/api/yii/base/action/index.md) object.

If this field is not set - no host name check will be performed.

### $denyCallback <span id="property-denyCallback"></span>

```php
public callable|null $denyCallback = null
```

A callback that will be called if the current host does not match [`$allowedHosts`](/docs/api/yii/filters/hostcontrol/index.md#property-allowedHosts).

If not set, [`denyAccess()`](/docs/api/yii/filters/hostcontrol/index.md#method-denyAccess) will be called.

The signature of the callback should be as follows:

```
function (\yii\base\Action $action)
```

where `$action` is the current [action](/docs/api/yii/base/action/index.md) object.

> Note: while implementing your own host deny processing, make sure you avoid usage of the current requested
host name, creation of absolute URL links, caching page parts and so on.

### $fallbackHostInfo <span id="property-fallbackHostInfo"></span>

```php
public string|null $fallbackHostInfo = ''
```

Fallback host info (e.g. `https://www.yiiframework.com`) used when [Request::$hostInfo](/docs/api/yii/web/request/index.md#property-hostInfo) is invalid.

This value will replace [Request::$hostInfo](/docs/api/yii/web/request/index.md#property-hostInfo) before [`$denyCallback`](/docs/api/yii/filters/hostcontrol/index.md#property-denyCallback) is called to make sure that
an invalid host will not be used for further processing. You can set it to `null` to leave [Request::$hostInfo](/docs/api/yii/web/request/index.md#property-hostInfo) untouched.
Default value is empty string (this will result creating relative URLs instead of absolute).

## Method details

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

```php
public function beforeAction(mixed $action): bool
```

This method is invoked right before an action is to be executed (after all possible filters.)
You may override this method to do last-minute preparation for the action.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | `mixed` | The action to be executed. |

**Returns**

`bool`: Whether the action should continue to be executed.

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

```php
protected function denyAccess(yii\base\Action $action): mixed
```

Denies the access.

The default implementation will display 404 page right away, terminating the program execution.
You may override this method, creating your own deny access handler. While doing so, make sure you
avoid usage of the current requested host name, creation of absolute URL links, caching page parts and so on.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$action` | [`yii\base\Action`](/docs/api/yii/base/action/index.md) | The action to be executed. |

**Returns**

`mixed`

**Throws**

- [`yii\web\NotFoundHttpException`](/docs/api/yii/web/notfoundhttpexception/index.md)