這篇文章主要為大家展示了“PHP如何實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“PHP如何實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能”這篇文章吧。

驗(yàn)證碼的校驗(yàn)是利用PHP中的 SESSION功能來實(shí)現(xiàn)。
在最頂端聲明函數(shù) session_start(); 告訴服務(wù)器我們要用這個(gè)函數(shù)的功能。
session_start();
接下來我們用到的就是驗(yàn)證碼實(shí)現(xiàn)的代碼。這里用英文數(shù)字的代碼為例。
$image = imagecreatetruecolor(100, 30); //創(chuàng)建一個(gè)100×30的畫布 $white = imagecolorallocate($image,255,255,255);//白色 imagefill($image,0,0,$white);//覆蓋黑色畫布
然后在驗(yàn)證碼實(shí)現(xiàn)之前聲明一個(gè)空變量,用來存放驗(yàn)證碼。
$session = ""; //空變量 ,存放驗(yàn)證碼
for($i=0;$i<4;$i++){
$size = 6;
$x = $i*25+mt_rand(5,10);
$y = mt_rand(5,10);
$sizi_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
$char = join("",array_merge(range('a','z'),range('A','Z'),range(0,9)));
$char = str_shuffle($char);
$char = substr($char,0,1);
imagestring($image,$size,$x,$y,$char,$sizi_color);
$session .= $char ; //把驗(yàn)證碼的每一個(gè)值賦值給變量
}
$_SESSION['session'] = $session; //這個(gè)變量的值與用戶輸入的值相等for($k=0;$k<200;$k++){
$rand_color = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,mt_rand(1,99),mt_rand(1,29),$rand_color);
}
for($n=0;$n<5;$n++){
$line_color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$line_color);
}
header('content-type:image/png');//設(shè)置文件輸出格式
imagepng( $image ); //以png格式輸出$image圖像
imagedestroy( $image ); //銷毀圖像用 POST 方式來接收驗(yàn)證碼。 strtolower 函數(shù)是讓服務(wù)器不區(qū)分大小寫。這樣可以有效減少用戶的輸錯(cuò)率。
if(isset($_POST['session'])){
session_start();
if(strtolower($_POST['session'])==strtolower($_SESSION['session'])){
echo'<font color="#0000CC">輸入正確</form>';
}else{
echo '<font color="#CC0000"><b>輸入錯(cuò)誤</b></font>';
}
exit();
}下面是HTML的頁面代碼。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>確認(rèn)驗(yàn)證碼</title>
</head>
<body>
<form method="post" action="./tushu.php">
<p>驗(yàn)證碼圖片:<img id="img" border="1" src="http://localhost//xxx.php" width="100" height="30"></p>
<a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('img').src='http://localhost//xxx.php'">看不清?換一個(gè)</a>
<p>請輸入圖片中的驗(yàn)證碼:<input type="text" name="session" value=""/></p>
<p><input type="submit" value="提交" ></p>
</form>
</body>
</html>這里特別說明一下 HTML代碼中加入了一個(gè)事件 onclick .當(dāng)用戶無法識(shí)別當(dāng)前驗(yàn)證碼的時(shí)候可以不用刷新瀏覽器,直接點(diǎn)擊“看不清?換一個(gè)”即可更換驗(yàn)證碼。

以上是“PHP如何實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞名稱:PHP如何實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://chinadenli.net/article36/gcdsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站策劃、企業(yè)建站、服務(wù)器托管、網(wǎng)站排名、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容