没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\captcha\CaptchaAction

继承yii\captcha\CaptchaAction » yii\base\Action » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/captcha/CaptchaAction.php

CaptchaAction renders a CAPTCHA image.

CaptchaAction is used together with yii\captcha\Captcha and yii\captcha\CaptchaValidator to provide the CAPTCHA feature.

By configuring the properties of CaptchaAction, you may customize the appearance of the generated CAPTCHA images, such as the font color, the background color, etc.

Note that CaptchaAction requires either GD2 extension or ImageMagick PHP extension.

Using CAPTCHA involves the following steps:

  1. Override yii\web\Controller::actions() and register an action of class CaptchaAction with ID 'captcha'
  2. In the form model, declare an attribute to store user-entered verification code, and declare the attribute to be validated by the 'captcha' validator.
  3. In the controller view, insert a yii\captcha\Captcha widget in the form.

公共属性

隐藏继承的属性

属性类型描述被定义在
$backColor integer The background color. yii\captcha\CaptchaAction
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$controller yii\base\Controller|yii\web\Controller|yii\console\Controller 拥有此操作的控制器 yii\base\Action
$fixedVerifyCode string The fixed verification code. yii\captcha\CaptchaAction
$fontFile string The TrueType font file. yii\captcha\CaptchaAction
$foreColor integer The font color. yii\captcha\CaptchaAction
$height integer The height of the generated CAPTCHA image. yii\captcha\CaptchaAction
$id string 动作的 ID yii\base\Action
$imageLibrary string The rendering library to use. yii\captcha\CaptchaAction
$maxLength integer The maximum length for randomly generated word. yii\captcha\CaptchaAction
$minLength integer The minimum length for randomly generated word. yii\captcha\CaptchaAction
$offset integer The offset between characters. yii\captcha\CaptchaAction
$padding integer Padding around the text. yii\captcha\CaptchaAction
$testLimit integer How many times should the same CAPTCHA be displayed. yii\captcha\CaptchaAction
$transparent boolean Whether to use transparent background. yii\captcha\CaptchaAction
$uniqueId string 整个应用程序中此操作的唯一 ID。 yii\base\Action
$verifyCode string The verification code. yii\captcha\CaptchaAction
$width integer The width of the generated CAPTCHA image. yii\captcha\CaptchaAction

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() 构造函数。 yii\base\Action
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
generateValidationHash() Generates a hash code that can be used for client-side validation. yii\captcha\CaptchaAction
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getUniqueId() 返回整个应用程序中此动作的唯一 ID。 yii\base\Action
getVerifyCode() Gets the verification code. yii\captcha\CaptchaAction
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the action. yii\captcha\CaptchaAction
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
run() Runs the action. yii\captcha\CaptchaAction
runWithParams() 使用指定的参数运行此动作。 该方法主要由控制器调用。 yii\base\Action
trigger() Triggers an event. yii\base\Component
validate() Validates the input to see if it matches the generated code. yii\captcha\CaptchaAction

受保护的方法

隐藏继承的方法

方法描述被定义在
afterRun() 执行 run() 后立即调用此方法。 您可以重写此方法以对动作运行执行后处理工作。 yii\base\Action
beforeRun() 在执行 run() 之前调用此方法。 您可以重写此方法来为动作运行做准备工作。 如果该方法返回 false,将取消该动作。 yii\base\Action
generateVerifyCode() Generates a new verification code. yii\captcha\CaptchaAction
getSessionKey() Returns the session variable name used to store verification code. yii\captcha\CaptchaAction
renderImage() Renders the CAPTCHA image. yii\captcha\CaptchaAction
renderImageByGD() Renders the CAPTCHA image based on the code using GD library. yii\captcha\CaptchaAction
renderImageByImagick() Renders the CAPTCHA image based on the code using ImageMagick library. yii\captcha\CaptchaAction
setHttpHeaders() 设置图像响应所需的 HTTP 头信息。 yii\captcha\CaptchaAction

常量

隐藏继承的常量

常量描述被定义在
REFRESH_GET_VAR 'refresh' The name of the GET parameter indicating whether the CAPTCHA image should be regenerated. yii\captcha\CaptchaAction

属性详情

$backColor 公共 属性

The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.

public integer $backColor 0xFFFFFF
$fixedVerifyCode 公共 属性

The fixed verification code. When this property is set, getVerifyCode() will always return the value of this property. This is mainly used in automated tests where we want to be able to reproduce the same verification code each time we run the tests. If not set, it means the verification code will be randomly generated.

public string $fixedVerifyCode null
$fontFile 公共 属性

The TrueType font file. This can be either a file path or path alias.

public string $fontFile '@yii/captcha/SpicyRice.ttf'
$foreColor 公共 属性

The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).

public integer $foreColor 0x2040A0
$height 公共 属性

The height of the generated CAPTCHA image. Defaults to 50.

public integer $height 50
$imageLibrary 公共 属性 (自版本 2.0.7 可用)

The rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.

public string $imageLibrary null
$maxLength 公共 属性

The maximum length for randomly generated word. Defaults to 7.

public integer $maxLength 7
$minLength 公共 属性

The minimum length for randomly generated word. Defaults to 6.

public integer $minLength 6
$offset 公共 属性

The offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.

public integer $offset = -2
$padding 公共 属性

Padding around the text. Defaults to 2.

public integer $padding 2
$testLimit 公共 属性

How many times should the same CAPTCHA be displayed. Defaults to 3. A value less than or equal to 0 means the test is unlimited (available since version 1.1.2).

public integer $testLimit 3
$transparent 公共 属性

Whether to use transparent background. Defaults to false.

public boolean $transparent false
$verifyCode 公共 只读 属性

The verification code.

public string getVerifyCode ( $regenerate false )
$width 公共 属性

The width of the generated CAPTCHA image. Defaults to 120.

public integer $width 120

方法详情

generateValidationHash() 公共 方法

Generates a hash code that can be used for client-side validation.

public string generateValidationHash($code)
$code string

The CAPTCHA code

return string

A hash code generated from the CAPTCHA code

generateVerifyCode() 受保护 方法

Generates a new verification code.

protected string generateVerifyCode()
return string

The generated verification code

getSessionKey() 受保护 方法

Returns the session variable name used to store verification code.

protected string getSessionKey()
return string

The session variable name

getVerifyCode() 公共 方法

Gets the verification code.

public string getVerifyCode($regenerate false)
$regenerate boolean

Whether the verification code should be regenerated.

return string

The verification code.

init() 公共 方法

Initializes the action.

public void init()
throws yii\base\InvalidConfigException

if the font file does not exist.

renderImage() 受保护 方法

Renders the CAPTCHA image.

protected string renderImage($code)
$code string

The verification code

return string

Image contents

throws yii\base\InvalidConfigException

if imageLibrary is not supported

renderImageByGD() 受保护 方法

Renders the CAPTCHA image based on the code using GD library.

protected string renderImageByGD($code)
$code string

The verification code

return string

Image contents in PNG format.

renderImageByImagick() 受保护 方法

Renders the CAPTCHA image based on the code using ImageMagick library.

protected string renderImageByImagick($code)
$code string

The verification code

return string

Image contents in PNG format.

run() 公共 方法

Runs the action.

public void run()
setHttpHeaders() 受保护 方法

设置图像响应所需的 HTTP 头信息。

protected void setHttpHeaders()
validate() 公共 方法

Validates the input to see if it matches the generated code.

public boolean validate($input, $caseSensitive)
$input string

User input

$caseSensitive boolean

Whether the comparison should be case-sensitive

return boolean

Whether the input is valid