php生成隨機(jī)驗(yàn)證碼圖片的示例代碼
1,CaptchaPicture.php用于生成畫(huà)布,然后在畫(huà)布上生成四位隨機(jī)驗(yàn)證碼
<?php
session_start();
header("Content-type:image/png"); //創(chuàng)建圖像的格式
$image_width=76; //設(shè)置圖像的寬度
$image_height=40; //設(shè)置圖像的高度
$length=4; //字符串長(zhǎng)度為4
//除去0,2,o,l容易混淆的字符
$str="23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSRUVWXYZ";
$code='';
for($i=0;$i<$length;$i++){
$code.=$str[mt_rand(0,strlen($str))-1]; //從字符串中隨機(jī)選擇
}
$_SESSION['verify']=$code; //從獲取到的隨機(jī)書(shū)驗(yàn)證碼寫(xiě)入到Session變量中
$image=imagecreate($image_width,$image_height); //創(chuàng)建一個(gè)畫(huà)布
imagecolorallocate($image,25,255,255); //設(shè)置畫(huà)布的顏色
For($i=0;$i<strlen($_SESSION['verify']);$i++){ //循環(huán)讀取Session變量中的驗(yàn)證碼
$font = mt_rand(3,5); //設(shè)置隨機(jī)的字體
$x=mt_rand(1,8)+$image_width*$i/4; //設(shè)置隨機(jī)字符所在位置的x坐標(biāo)
$y=mt_rand(8,$image_height/4); //設(shè)置隨機(jī)字符所在位置的y坐標(biāo)
//設(shè)置字符顏色
$color = imagecolorallocate($image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($image,$font,$x,$y,$_SESSION['verify'][$i],$color); //水平輸出字符
}
//繪制干擾元素點(diǎn)
$pixel=30;
$black = imagecolorallocate($image,0,0,0);
for($i=0;$i<$pixel;$i++){
imagesetpixel($image,mt_rand(0,$image_width-1),mt_rand(0,$image_height-1),$black);
}
imagepng($image); //生成png格式的圖像
imagedestroy($image); //釋放圖片資源
?>2,SubmitVerify.php用于做一個(gè)簡(jiǎn)單的測(cè)試,驗(yàn)證驗(yàn)證碼功能是否正常
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="CheckLogin.php" method="post">
<div class="verify">
<input type="text" id="verify" name="verify" class="input" placeholder="驗(yàn)證碼">
<a>
<img src="CaptchPicture.php" onclick="this.src=this.src+'?'+Math.random()"> //單擊驗(yàn)證碼生成新的驗(yàn)證碼
</a>
</div>
<button type="submit">提交</button>
</form>
</body>
</html>3,CheckLogin.php用于驗(yàn)證提交后是否能接受到傳遞的信息,以及判斷輸入的驗(yàn)證碼和生成的驗(yàn)證碼是否一致
<?php
session_start();
$checks = $_POST["verify"]; //獲取表單提交的驗(yàn)證碼(用戶輸入的驗(yàn)證碼)
if ($checks=="") {
echo "<script>alert('驗(yàn)證碼不能為空');
window.location.href='SubmitVerify.php';</script>";
}
if ($checks==$_SESSION["verify"]){ //這個(gè)獲取的是系統(tǒng)隨機(jī)生成的驗(yàn)證碼
echo "<script>alert('登錄成功');</script>";
}else{
echo "<script>alert('你輸入的驗(yàn)證碼不正確!')
window.location.href='SubmitVerify.php'
</script>";
}到此這篇關(guān)于php生成隨機(jī)驗(yàn)證碼圖片的示例代碼的文章就介紹到這了,更多相關(guān)php生成隨機(jī)驗(yàn)證碼圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php實(shí)現(xiàn)四舍五入的方法小結(jié)
這篇文章主要介紹了php實(shí)現(xiàn)四舍五入的方法,實(shí)例總結(jié)了php實(shí)現(xiàn)四舍五入的三種常用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
PHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法
PDO(PHP Data Object) 是PHP 5 中加入的東西,是PHP 5新加入的一個(gè)重大功能,因?yàn)樵赑HP 5以前的php4/php3都是一堆的數(shù)據(jù)庫(kù)擴(kuò)展來(lái)跟各個(gè)數(shù)據(jù)庫(kù)的連接和處理,什么 php_mysql.dll、php_pgsql.dll、php_mssql.dll、php_sqlite.dll等等。2010-08-08

