php實現(xiàn)算術(shù)驗證碼功能
最近學(xué)了php的圖像函數(shù),也練習(xí)的驗證碼的實現(xiàn),在聽從老師的建議下,自己寫了一個算術(shù)驗證碼的函數(shù),第一次自己獨立完成一個小練習(xí),感覺挺開心的。但是,也知道自己的局限,所以特意寫出來,希望各路大神指點一下,能夠更多的拓展自己的視野。
php代碼如下:實現(xià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);
//干擾點
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);
}
//定義一個數(shù)組存放運算符號
$arr = ['+','-','*'];
//計算數(shù)組的長度
$len = count($arr);
//定義一個1到20的數(shù)組
$num = range(1,20);
$numLen = count($num);
//定義一個空數(shù)組來存放隨機(jī)取得的驗證碼
$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 一個包含運算符號的數(shù)組
* @return 返回一個運算結(jié)果
*/
function getRes($arr) {
$sum = 0;
//判斷數(shù)組元素下標(biāo)為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;
}
//調(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">
驗證碼 <input type="text" name="code">
<img src="./demo5.php" alt="點擊刷新">
<br>
<button>提交</button>
</form>
</body>
</html>
<script>
//實現(xiàn)點擊圖片刷新驗證碼的功能
var img = document.querySelector("img");
img.onclick = function () {
this.src = this.src+"?m="+Math.random();
}
</script>
test.php的測試
<?php
session_start();
$res = $_SESSION['res'];
$value = $_GET['code'];
if ($res == $value) {
echo "test success";
}else{
echo "test fail";
}
以上就是我的代碼組成.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php實現(xiàn)有序數(shù)組旋轉(zhuǎn)后尋找最小值方法
在本篇文章中我們給大家詳細(xì)分享了php實現(xiàn)有序數(shù)組旋轉(zhuǎn)后尋找最小值方法,有需要的朋友們可以學(xué)習(xí)下。2018-09-09
PHP函數(shù)之error_reporting(E_ALL ^ E_NOTICE)詳細(xì)說明
在看帝國cms的connect.php是發(fā)現(xiàn)第一句是error_reporting(E_ALL ^ E_NOTICE);以前也沒注意過這個語句,知道是設(shè)置錯誤提示的,但不清楚具體怎樣設(shè)置使用。下面從網(wǎng)上摘抄了些東西,總結(jié)了一下。2011-07-07
php多進(jìn)程模擬并發(fā)事務(wù)產(chǎn)生的問題小結(jié)
這篇文章主要給大家介紹了關(guān)于php多進(jìn)程模擬并發(fā)事務(wù)產(chǎn)生的問題,文中通過示例代碼介紹的非常想吃詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
php根據(jù)一個給定范圍和步進(jìn)生成數(shù)組的方法
這篇文章主要介紹了php根據(jù)一個給定范圍和步進(jìn)生成數(shù)組的方法,涉及php針對數(shù)組的遍歷技巧,需要的朋友可以參考下2015-06-06
PHP新手用的Insert和Update語句構(gòu)造類
PHP新手用的Insert和Update語句構(gòu)造類,沒多大功能,學(xué)習(xí)php的朋友可以參考下2012-03-03

