Skip to content
PHP Ecosystem
Explore

HostControl

API reference › yii › filters › HostControl

HostControl provides simple control over requested host name.

This filter provides protection against ‘host header’ attacks, 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.

PropertyTypeDescriptionDefined by
$allowedHostsarray|Closure|nullList of host names, which are allowed. Default: null.HostControl
$denyCallbackcallable|nullA callback that will be called if the current host does not match $allowedHosts. Default: null.HostControl
$exceptarrayList of action IDs that this filter should not apply to. Default: [].yii\base\ActionFilter
$fallbackHostInfostring|nullFallback host info (e.g. https://www.yiiframework.com) used when Request::$hostInfo is invalid. Default: ''.HostControl
$onlyarrayList of action IDs that this filter should apply to. Default: [].yii\base\ActionFilter
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
afterAction()This method is invoked right after an action is executed.yii\base\ActionFilter
afterFilter()yii\base\ActionFilter
attach()Attaches the behavior object to the component.yii\base\ActionFilter
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
beforeFilter()yii\base\ActionFilter
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\ActionFilter
events()Declares event handlers for the $owner’s events.yii\base\Behavior
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
denyAccess() protectedDenies the access.HostControl
getActionId() protectedReturns an action ID by converting Action::$uniqueId into an ID relative to the module.yii\base\ActionFilter
isActive() protectedReturns a value indicating whether the filter is active for the given action.yii\base\ActionFilter
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 object.

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

public callable|null $denyCallback = null

A callback that will be called if the current host does not match $allowedHosts.

If not set, denyAccess() will be called.

The signature of the callback should be as follows:

function (\yii\base\Action $action)

where $action is the current action 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.

public string|null $fallbackHostInfo = ''

Fallback host info (e.g. https://www.yiiframework.com) used when Request::$hostInfo is invalid.

This value will replace Request::$hostInfo before $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 untouched. Default value is empty string (this will result creating relative URLs instead of absolute).

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

ParameterTypeDescription
$actionmixedThe action to be executed.

Returns

bool: Whether the action should continue to be executed.

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

ParameterTypeDescription
$actionyii\base\ActionThe action to be executed.

Returns

mixed

Throws