Class yii\captcha\CaptchaAction
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:
- Override yii\web\Controller::actions() and register an action of class CaptchaAction with ID 'captcha'
- In the form model, declare an attribute to store user-entered verification code, and declare the attribute to be validated by the 'captcha' validator.
- In the controller view, insert a yii\captcha\Captcha widget in the form.
公共属性
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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 |
属性详情
The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
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.
The TrueType font file. This can be either a file path or path alias.
The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
The height of the generated CAPTCHA image. Defaults to 50.
The rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.
The maximum length for randomly generated word. Defaults to 7.
The minimum length for randomly generated word. Defaults to 6.
The offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.
Padding around the text. Defaults to 2.
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).
Whether to use transparent background. Defaults to false.
The verification code.
The width of the generated CAPTCHA image. Defaults to 120.
方法详情
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 |
---|
Generates a new verification code.
protected string generateVerifyCode() | ||
return | string | The generated verification code |
---|
Returns the session variable name used to store verification code.
protected string getSessionKey() | ||
return | string | The session variable name |
---|
Gets the verification code.
public string getVerifyCode($regenerate = false) | ||
$regenerate | boolean | Whether the verification code should be regenerated. |
return | string | The verification code. |
---|
Initializes the action.
public void init() | ||
throws | yii\base\InvalidConfigException | if the font file does not exist. |
---|
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 |
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. |
---|
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. |
---|
Runs the action.
public void run() |
设置图像响应所需的 HTTP 头信息。
protected void setHttpHeaders() |
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 |
---|