本文目錄一覽:
求助!!在php中想實現圖片驗證碼的效果
1.rand一個數串。
2.存入cookie中或session中。
3.將數用gd庫方法生成圖片。
4.用戶輸入後與cookie或session對比。
done
怎麼用php生成圖像,生成驗證碼
?php
//驗證碼類
class ValidateCode {
private $charset = ‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789’;//隨機因子
private $code;//驗證碼
private $codelen = 4;//驗證碼長度
private $width = 90;//寬度
private $height = 40;//高度
private $img;//圖形資源句柄
private $font;//指定的字體
private $fontsize = 20;//指定字體大小
private $fontcolor;//指定字體顏色
//構造方法初始化
public function __construct() {
$this-font = dirname(__FILE__).’/font/elephant.ttf’;//注意字體路徑要寫對,否則顯示不了圖片
}
//生成隨機碼
private function createCode() {
$_len = strlen($this-charset)-1;
for ($i=0;$i$this-codelen;$i++) {
$this-code .= $this-charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
$this-img = imagecreatetruecolor($this-width, $this-height);
$color = imagecolorallocate($this-img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this-width / $this-codelen;
for ($i=0;$i$this-codelen;$i++) {
$this-fontcolor = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);
}
}
//生成線條、雪花
private function createLine() {
//線條
for ($i=0;$i6;$i++) {
$color = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);
}
//雪花
for ($i=0;$i100;$i++) {
$color = imagecolorallocate($this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-height),’*’,$color);
}
}
//輸出
private function outPut() {
header(‘Content-type:image/png’);
imagepng($this-img);
imagedestroy($this-img);
}
//對外生成
public function doimg() {
$this-createBg();
$this-createCode();
$this-createLine();
$this-createFont();
$this-outPut();
}
//獲取驗證碼
public function getCode() {
return strtolower($this-code);
}
}
如何解決thinkphp5中驗證碼常見問題
在項目目錄下面生成captcha擴展 (需要安裝composer來安裝)
composer require topthink/think-captcha
2 安裝完成之後會呈現如上的目錄。captcha擴展安裝完成之後,就可以進行下一步操作
配置comfig.php文件:在comfig.php下面加個擴展
//驗證碼
‘captcha’= [
//字符集合
‘codeset’=’23456780qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM’,
//字體大小
‘fontSize’ = 18,
//是否花混淆曲線
‘useCurve’ = true,
//圖片高度
‘imageH’ = 40,
//圖片寬度
‘imageW’ = 130,
//位數
‘length’ = 4,
//驗證成功後是否重置
‘reset’ = true,
],
3.刷新驗證碼功能 src路徑在這裡可以用框架自帶的也可以直接訪問img方法
img id=”captcha_img” src=”{:captcha_src()}” alt=”驗證碼” onclick=”refreshVerify()”a
href=”javascript:refreshVerify()”點擊刷新/a
在js《script》標籤部分加上刷新事件
function refreshVerify()
{
var ts = Date.parse(new Date() )/1000;
console.log(ts);
$(‘#captcha_img’).attr(‘src’,’/captcha?id=’+ts);
}
4.在控制器里用TP驗證自帶的方法
在你的登錄的控制器裡面加入
4.1在頭部引入 use think\captcha\Captcha;
//該方法引入img圖像 寬高可以再img用css直接控制!
public function img() {
$captcha = new Captcha();
return $captcha-entry();
}
// 檢測輸入的驗證碼是否正確,$code為用戶輸入的驗證碼字元串,$id多個驗證碼標識
function check_verify($code, $id = ”){
$captcha = new Captcha();
return $captcha-check($code, $id);
}這個方法下面的驗證要用到
在你的form驗證碼值發送的方法里加入 post提交 GET提交就把post改成get 表單驗證碼name是code
$request=request();
if ($request-isPost()){
if($request-post(‘code’)){
if($this-check_verify($request-post(‘code’))){
$message= ‘驗證成功’;
}else{
$message= ‘驗證錯誤’;
}
}else{
$message= ‘沒有輸入驗證碼’;
}
}
照以上做沒有任何問題
php驗證碼怎麼實現
1. 新建code.php驗證碼生成文件
在此之前必須打開php的GD庫,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分號。代碼如下:
?php
session_start();
//生成驗證碼圖片
Header(“Content-type: image/PNG”);
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景
srand((double)microtime()*1000000);
//生成4位數字
for($i=0;$i4;$i++){
$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
$authnum=rand(1,9);
$vcodes.=$authnum;
imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}
for($i=0;$i100;$i++) //加入干擾象素
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()p , rand()0 , $randcolor);
}
ImagePNG($im);
ImageDestroy($im);
$_SESSION[‘Checknum’] = $vcodes;
?
2. 顯示驗證碼圖片
在需要顯示驗證碼的頁面中加入
input type=”text” name=”passcode”
img src=”code.php”
3.判斷並獲取驗證碼的值
驗證碼是通過第一步驟代碼中的$_SESSION[‘Checknum’] = $vcodes;賦的值,所以驗證碼的值存在$_SESSION[‘Checknum’]當中。在驗證頁面,使用以下代碼,
…
session_start();//啟動會話
$code=$_POST[“passcode”];
if( $code == $_SESSION[“Checknum”])
{…}即可完成驗證碼登錄。
運行截圖:
望採納,謝謝
原創文章,作者:ZKFKY,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316442.html