溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php怎么實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼

發(fā)布時(shí)間:2021-10-21 17:02:29 來源:億速云 閱讀:156 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“php怎么實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php怎么實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼”吧!

php實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼的方法:【session_start(); $width = 150; $height = 40; $image = imagecreatetruecolor($width, $height);$bgcol...】。

php怎么實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼

本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

現(xiàn)在驗(yàn)證碼在表單中的應(yīng)用越來越多了,但是如果用js來實(shí)現(xiàn)總覺得不太方便,因此使用php來實(shí)現(xiàn)下,在此記錄下。

當(dāng)然,我們也可以封裝成一個(gè)函數(shù),以后使用的時(shí)候也是很方便的,這里并未封裝,感興趣的小伙伴可以自己封裝下。

具體實(shí)現(xiàn)代碼:

新建一個(gè)cap_sz.php文件:

<?php
session_start(); //設(shè)置session,一定要在頂部

$width = 150; //設(shè)置圖片寬為300像素
$height = 40; //設(shè)置圖片高為40像素

$image = imagecreatetruecolor($width, $height); //設(shè)置驗(yàn)證碼大小的函數(shù)
$bgcolor = imagecolorallocate($image, 255, 255, 255); //驗(yàn)證碼顏色RGB為(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //區(qū)域填充

$cap_code = "";
for($i=0;$i<4;$i++){
    $fontsize = 7; //設(shè)置字體大小
    $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    //數(shù)字越大,顏色越淺,這里是深顏色0-120
    $fontcontent = rand(0,9);
    $cap_code.=$fontcontent; //.=連續(xù)定義變量
   
    $x = ($i*150/4)+rand(5,10);
    $y = rand(5,10);
    //設(shè)置坐標(biāo)
   
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}

$_SESSION['code'] = $cap_code; //存到session

//設(shè)置干擾元素,設(shè)置雪花點(diǎn)
for($i=0;$i<300;$i++){
    $inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
    //設(shè)置顏色,20-200顏色比數(shù)字淺,不干擾閱讀
    imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
    //畫一個(gè)單一像素的元素
}

//增加干擾元素,設(shè)置橫線(先設(shè)置線的顏色,在設(shè)置橫線)
for ($i=0;$i<4;$i++) {
    $linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
    //設(shè)置線的顏色

    imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);  

}

//因?yàn)橛行g覽器,訪問的content-type會(huì)是文本型(亂碼),所以我們需要設(shè)置成圖片的格式類型
header('Content-Type:image/png');

imagepng($image); //建立png函數(shù)
imagedestroy($image); //結(jié)束圖形函數(shù),消除$image

然后新建一個(gè)index.php進(jìn)行驗(yàn)證

<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_REQUEST['code'])){
    session_start();

    if ($_REQUEST['code'] == $_SESSION['code']){
        echo "輸入正確";
    }else{
        echo "輸入錯(cuò)誤,請重新輸入";
    }

    exit();
}

?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //這里不能少,不然亂碼
    <title>驗(yàn)證碼測試</title>
</head>
<body>
<form>
<p>驗(yàn)證碼:<img src="cap_sz.php" onClick="this.src='cap_sz.php?nocache='+Math.random()" style="cursor:hand" alt="點(diǎn)擊換一張"/>點(diǎn)擊圖片可更換驗(yàn)證碼</p>
<p>請輸入圖片中的內(nèi)容:<input type="text" name="code" value=""/></p>
<p><input type="submit" width="20px" height=19px value="提交"></input></p>

</form>
</body>
</html>

到此,相信大家對(duì)“php怎么實(shí)現(xiàn)自動(dòng)生成驗(yàn)證碼”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI