驗證碼在表單實現越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實現的驗證碼
驗證碼在表單實現越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實現的驗證碼。當然,也可以封裝成一個函數,以後使用的時候也是很方便的,但是現在未封裝。本人菜鳥一隻,大佬勿噴,也希望得到大佬的意見
現在說說簡單的數字驗證吧
建議各位按照代碼註釋一步一步來,改少的分號不能少。
新建一個cap_sz.php文件:
<?php
session_start(); //設置session,一定要在頂部
$width = 150; //設置圖片寬爲300像素
$height = 40; //設置圖片高爲40像素
$image = imagecreatetruecolor($width, $height); //設置驗證碼大小的函數
$bgcolor = imagecolorallocate($image, 255, 255, 255); //驗證碼顏色RGB爲(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //區域填充
$cap_code = "";
for($i=0;$i<4;$i++){
$fontsize = 7; //設置字體大小
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
//數字越大,顏色越淺,這裏是深顏色0-120
$fontcontent = rand(0,9);
$cap_code.=$fontcontent; //.=連續定義變量
$x = ($i*150/4)+rand(5,10);
$y = rand(5,10);
//設置座標
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['code'] = $cap_code; //存到session
//設置干擾元素,設置雪花點
for($i=0;$i<300;$i++){
$inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
//設置顏色,20-200顏色比數字淺,不干擾閱讀
imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
//畫一個單一像素的元素
}
//增加干擾元素,設置橫線(先設置線的顏色,在設置橫線)
for ($i=0;$i<4;$i++) {
$linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
//設置線的顏色
imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);
}
//因爲有些瀏覽器,訪問的content-type會是文本型(亂碼),所以我們需要設置成圖片的格式類型
header('Content-Type:image/png');
imagepng($image); //建立png函數
imagedestroy($image); //結束圖形函數,消除$image
然後新建一個index.php進行驗證
<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_REQUEST['code'])){
session_start();
if ($_REQUEST['code'] == $_SESSION['code']){
echo "輸入正確";
}else{
echo "輸入錯誤,請重新輸入";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //這裏不能少,不然亂碼
<title>驗證碼測試</title>
</head>
<body>
<form>
<p>驗證碼:<img src="cap_sz.php" onClick="this.src='cap_sz.php?nocache='+Math.random()" style="cursor:hand" alt="點擊換一張"/>點擊圖片可更換驗證碼</p>
<p>請輸入圖片中的內容:<input type="text" name="code" value=""/></p>
<p><input type="submit" width="20px" height=19px value="提交"></input></p>
</form>
</body>
</html>
數字測試:
正確輸入:
錯誤輸入:
然後我們實現以下大小寫英文加數字的驗證
新建一個cap_zy.php文件(從上面複製一份)
<?php
session_start(); //設置session
$width = 150; //設置圖片寬爲300像素
$height = 40; //設置圖片高爲40像素
$image = imagecreatetruecolor($width, $height); //設置驗證碼大小的函數
$bgcolor = imagecolorallocate($image, 255, 255, 255); //驗證碼顏色RGB爲(255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); //區域填充
$cap_code = "";
for($i=0;$i<4;$i++){
$fontsize = 8; //設置字體大小
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
//數字越大,顏色越淺,這裏是深顏色0-120
$data = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789'; //添加字符串
$fontcontent = substr($data, rand(0,strlen($data)),1); //去除值,字符串截取方法
$cap_code.=$fontcontent; //.=連續定義變量
$x = ($i*150/4)+rand(5,10);
$y = rand(5,10);
//設置座標
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['code'] = $cap_code; //存到session
//設置干擾元素,設置雪花點
for($i=0;$i<300;$i++){
$inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
//設置顏色,20-200顏色比數字淺,不干擾閱讀
imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
//畫一個單一像素的元素
}
//增加干擾元素,設置橫線(先設置線的顏色,在設置橫線)
for ($i=0;$i<4;$i++) {
$linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
//設置線的顏色
imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);
}
//因爲有些瀏覽器,訪問的content-type會是文本型,所以我們需要設置成圖片的格式類型
header('Content-Type:image/png');
imagepng($image); //建立png函數
imagedestroy($image);
然後修改上面的index.php文件進行驗證
<?php
header("Content-Type: text/html;charset=utf-8");
if (isset($_REQUEST['code'])){
session_start();
if ($_REQUEST['code'] == $_SESSION['code']){
echo "輸入正確";
}else{
echo "輸入錯誤,請重新輸入";
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>驗證碼測試</title>
</head>
<body>
<form>
<p>驗證碼:<img src="cap_zy.php" onClick="this.src='cap_zy.php?nocache='+Math.random()" style="cursor:hand" alt="點擊換一張"/>點擊圖片可更換驗證碼</p>
<p>請輸入圖片中的內容:<input type="text" name="code" value=""/></p>
<p><input type="submit" width="20px" height=19px value="提交"></input></p>
</form>
</body>
</html>
中英文驗證碼驗證:
輸入正確:
輸入錯誤:
這就完美的實現了!