Detailed interpretation codes ThinkPHP5.1

Composer must first ensure that you have installed, and then open a command line execute the following statement

composer require topthink/think-captcha=2.0.*

There are a lot of people do not pay attention pit, its expansion pack version 5.1 and version 5.0 are different command to install, do not install the wrong, wrong security can not be used for the 5.0 version of the command to install the following commands
composer require topthink / think-captcha
after installing it for the next step, we have to write HTML code, img src tag is used to display the image verification code, be sure to write right. Id and add a click event onclick img years.

<div class="form-group">
	<label for="inputPassword3" class="col-sm-2 control-label">验证码</label>
		<div class="col-sm-3">
			<input type="text" class="form-control" name="verify" placeholder="验证码">
		</div>
			<img id="verify_img" src="{:captcha_src()}" style="width: 150px;" onclick="refreshVerify()">
</div>

Next, add a js code to refresh the code, as follows, after the completion of the verification code it by clicking refresh,

<script>
	function refreshVerify() {
		var ts=Date.parse(new Date())/1000;//返回当前时间的事件戳除于1000
		$("#verify_img").attr("src","/captcha?rand="+ts);//刷新验证码
	}
</script>

Verification code is correct inherited validate class by adding a class

You can write a check for a verification code.

 protected $rule = [
        'verify'  => 'require|captcha',
];
Published 15 original articles · won praise 11 · views 2801

Guess you like

Origin blog.csdn.net/YCarlos/article/details/91076117