2019-04-15 10:54:28 3155瀏覽
本篇文章扣丁學(xué)堂PHP培訓(xùn)小編給讀者們分享一下PHP如何實(shí)現(xiàn)算術(shù)驗(yàn)證碼功能,文中有代碼列出供大家參考學(xué)習(xí),希望和小伙伴們共同參考。
php代碼如下:實(shí)現(xiàn)驗(yàn)證碼的功能:
<?php /** * @param int $width 寬度,默認(rèn)為120 * @param int $height 高度,默認(rèn)為50 * @param int $fontSize 字體的大小 * @return 圖片資源 */ function arithmeticCode($width=120,$height=50,$fontSize=20){ //開啟session session_start(); //創(chuàng)建畫布 $img = imagecreatetruecolor($width,$height); //分配顏色 $color = imagecolorallocate($img,255,255,255); //填充顏色 imagefill($img,0,0,$color); //干擾點(diǎn) for ($i = 0;$i < 500;$i++){ $pixColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),$pixColor); } //干擾線 for ($i = 0;$i < 4;$i++){ $lineColor = imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor); } //定義一個(gè)數(shù)組存放運(yùn)算符號(hào) $arr = ['+','-','*']; //計(jì)算數(shù)組的長(zhǎng)度 $len = count($arr); //定義一個(gè)1到20的數(shù)組 $num = range(1,20); $numLen = count($num); //定義一個(gè)空數(shù)組來存放隨機(jī)取得的驗(yàn)證碼 $code = []; for ($i = 0;$i < $len;$i++) { if ($i == 1) { $code[] = $arr[mt_rand(0,$len-1)]; }else { $code[] = $num[mt_rand(0,$numLen-1)]; } } $str = implode($code);//將數(shù)組轉(zhuǎn)為字符串 $textColor = imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); $fontAngle = 0; $x = ($width - $fontSize*3)/2; $y = ($height - $fontSize) / 2 + $fontSize; imagettftext($img,$fontSize,$fontAngle,$x,$y,$textColor,"./img/msyh.ttc",$str); $res = getRes($code); //將函數(shù)存放在session中 $_SESSION['res'] = $res; //輸出圖片 header("content-type:image/png"); imagepng($img); } /** * @param $arr 一個(gè)包含運(yùn)算符號(hào)的數(shù)組 * @return 返回一個(gè)運(yùn)算結(jié)果 */ function getRes($arr) { $sum = 0; //判斷數(shù)組元素下標(biāo)為1的運(yùn)算符號(hào)是什么 switch ($arr[1]){ case '+': $sum = $arr[0] + $arr[2]; break; case '-': $sum = $arr[0] - $arr[2]; break; case '*': $sum = $arr[0] * $arr[2]; break; } return $sum; } //調(diào)用函數(shù) arithmeticCode(100,40,18);
html的部分代碼:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> img { position: relative; top: 20px; } </style> </head> <body> <form action="test.php"> 驗(yàn)證碼 <input type="text" name="code"> <img src="./demo5.php" alt="點(diǎn)擊刷新"> <br> <button>提交</button> </form> </body> </html> <script> //實(shí)現(xiàn)點(diǎn)擊圖片刷新驗(yàn)證碼的功能 var img = document.querySelector("img"); img.onclick = function () { this.src = this.src+"?m="+Math.random(); } </script>
test.php的測(cè)試:
<?php session_start(); $res = $_SESSION['res']; $value = $_GET['code']; if ($res == $value) { echo "test success"; }else{ echo "test fail"; }
想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂PHP培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的PHP講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂PHP技術(shù)交流群:374332265。
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開發(fā)免費(fèi)公開課】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>