国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php實現算術驗證碼功能

2024-05-05 00:05:51
字體:
來源:轉載
供稿:網友

最近學了php的圖像函數,也練習的驗證碼的實現,在聽從老師的建議下,自己寫了一個算術驗證碼的函數,第一次自己獨立完成一個小練習,感覺挺開心的。但是,也知道自己的局限,所以特意寫出來,希望各路大神指點一下,能夠更多的拓展自己的視野。

php代碼如下:實現驗證碼的功能

<?php/** * @param int $width 寬度,默認為120 * @param int $height 高度,默認為50 * @param int $fontSize 字體的大小 * @return 圖片資源 */function arithmeticCode($width=120,$height=50,$fontSize=20){  //開啟session  session_start();  //創建畫布  $img = imagecreatetruecolor($width,$height);  //分配顏色  $color = imagecolorallocate($img,255,255,255);  //填充顏色  imagefill($img,0,0,$color);  //干擾點  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);  }  //定義一個數組存放運算符號  $arr = ['+','-','*'];  //計算數組的長度  $len = count($arr);  //定義一個1到20的數組  $num = range(1,20);  $numLen = count($num);  //定義一個空數組來存放隨機取得的驗證碼  $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);//將數組轉為字符串  $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);  //將函數存放在session中  $_SESSION['res'] = $res;  //輸出圖片  header("content-type:image/png");  imagepng($img);}/** * @param $arr 一個包含運算符號的數組 * @return 返回一個運算結果 */function getRes($arr) {  $sum = 0;  //判斷數組元素下標為1的運算符號是什么  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;}//調用函數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">  驗證碼 <input type="text" name="code">  <img src="./demo5.php"  <br>  <button>提交</button></form></body></html><script>  //實現點擊圖片刷新驗證碼的功能  var img = document.querySelector("img");  img.onclick = function () {    this.src = this.src+"?m="+Math.random();  }</script>

test.php的測試

<?phpsession_start();$res = $_SESSION['res'];$value = $_GET['code'];if ($res == $value) {  echo "test success";}else{  echo "test fail";}

以上就是我的代碼組成. 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 博乐市| 县级市| 商洛市| 罗定市| 花莲县| 隆昌县| 精河县| 凤台县| 松潘县| 三都| 平阴县| 新沂市| 体育| 岗巴县| 丰都县| 崇明县| 海原县| 会泽县| 大悟县| 军事| 铜山县| 西宁市| 怀仁县| 泰兴市| 阿坝县| 个旧市| 龙陵县| 织金县| 长武县| 通化市| 寻乌县| 酉阳| 都安| 呈贡县| 赣州市| 大邑县| 华蓥市| 喜德县| 水富县| 株洲县| 华蓥市|