1. 程式人生 > >ThinkPHP 5 驗證碼實現和驗證

ThinkPHP 5 驗證碼實現和驗證

來自公共檔案  common.php

/**
 * [cx_validatecode_img 生成驗證碼]
 * @param  string $imgparam [description]
 * @param  string $imgattrs [description]
 * @return [type]           [description]
 */
function cx_validatecode_img($imgparam='',$imgattrs='title="點選獲取"'){
	$src= "/login/checkcode/".$imgparam;
	$img=<<<hello
<img src="$src" onclick="this.src='$src&time='+Math.random();" $imgattrs/>
hello;
	return $img;
}


PHP程式碼  來自Login控制器

/**
 * [checkcode 登陸驗證碼操作方法]
 * @return [type] [description]
 */
public function checkcode(){
	$captcha = new \think\captcha\Captcha();        
	$captcha->imageW=121;        
	$captcha->imageH = 32;  //圖片高        
	$captcha->fontSize =14;  //字型大小        
	$captcha->length   = 4;  //字元數        
	// $captcha->fontttf = '5.ttf';  //字型        
	$captcha->expire = 30;  //有效期        
	$captcha->useNoise = false;  //不新增雜點        
	$captcha->reset = true;  //驗證成功後是否重置        
	return $captcha->entry();
}

HTML 來自login.html 
<tr>
	<td>驗證碼:</td>
	<td>{:cx_validatecode_img()}</td>
	<td><input type="text" name="validate" placeholder="請輸入驗證碼"></td>
</tr>