php实现验证码正确输入_PHP实现验证码
驗證碼是什么
驗證碼為全自動區分人和計算機的圖靈測試的縮寫,是一種區分用戶是計算機或人的公共全自動程序。
驗證碼應用場景
登錄、注冊確定提交之前,做人/及其校驗
發布、回復信息之前,做人/及其校驗
疑似機器請求時,做人/及其校驗
…
驗證碼實現步驟
生成底圖
生成驗證內容
生成驗證碼圖片
校驗驗證內容
所用到的PHP函數
imagecreatetruecolor ( int $width, int $height )
//創建一個寬$width高$height的畫布
imagecolorallocate ( resource $image, int $red, int $green, int $blue )
//為畫布分配顏色,red,green 和 blue分別是所需要的顏色的RGB值
imagefill ( resource $image, int $x, int $y, int $color )
//將分配好的顏色填充到畫布
imagepng(resource $image [, string filename])
//輸出一張png格式的圖片
imagestring ( resource $image, int $fontsize, int $x, int $y, string $content, int $color )
//在畫布上面水平寫上文字
imagesetpixel ( resource $image, int $x, int $y, int $color )
//畫一個點或者一個單一像素
imageline ( resource $image, int $x1, int $y1, int $x2, int $y2, int $color )
//畫一條線段
substr ( string $string, int $start, int $length )
//詳情請查看手冊
imagedestroy ( $image )
//銷毀圖像資源,釋放與 image 關聯的內存
目標1
通過PHP代碼,生成一張100*30px大小的圖片
TIPS
依賴GD擴展
輸出圖片前必須提前輸出圖片header信息
該方法默認輸出為黑色背景
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width,$height);
$pale = imagecolorallocate($image,204,204,204);
imagefill($image,0,0, $pale);
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
目標2
在底圖上顯示隨機數字
TIPS
注意好字體大小和分布,避免字體重疊或顯示不全
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width,$height);
$pale = imagecolorallocate($image,204,204,204);
imagefill($image,0,0, $pale);
$length = 4;
for ($i=1;$i<=$length;$i++){
$fontsize = 6;
$fontcontent = mt_rand(0,9);
$x = $i*100/5;
$y = mt_rand(5,10);
$fontcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>
目標3
為驗證碼增加干擾元素,干擾的點或線
TIPS
干擾信息一定要控制好顏色,避免喧賓奪主
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image,204,204,204);
imagefill($image,0,0,$bgcolor);
$length = 4;
for ($i=1;$i<=$length;$i++){
$fontsize = 6;
$x = $i*100/5;
$y = mt_rand(5,10);
$fontcontent = mt_rand(0,9);
$fontcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$pointnum = 200;
for ($i=1;$i<=200;$i++){
$x = mt_rand(1,99);
$y = mt_rand(1,29);
$pointcolor = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,$x,$y,$pointcolor);
}
$linenum = 3;
for ($i=1;$i<=$linenum;$i++){
$linecolor = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($iamge);
目標4
讓圖片上的驗證碼內容顯示為字母或數字、字母的混合體
TIPS
N/A
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image,204,204,204);
imagefill($image,0,0,$bgcolor);
$length = 4;
for ($i=1;$i<=$length;$i++){
$fontsize = 6;
$x = $i*100/5;
$y = mt_rand(5,10);
$data = 'ABCDEFGHIGKMNOPQRSTUVWXYabcdefghijkmnopqrstuvwxy34567890';
$fontcontent = substr($data,mt_rand(0,strlen($data)),1);
$fontcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$pointnum = 200;
for ($i=1;$i<=200;$i++){
$x = mt_rand(1,99);
$y = mt_rand(1,29);
$pointcolor = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,$x,$y,$pointcolor);
}
$linenum = 3;
for ($i=1;$i<=$linenum;$i++){
$linecolor = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$linecolor);
}
ob_clean();
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
目標5
在服務器端記錄驗證碼信息,便于用戶輸入后做校驗
TIPS
session_start()必須處于腳本最頂部
多服務器情況,需要考慮集中管理session信息
session_start();
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image,204,204,204);
imagefill($image,0,0,$bgcolor);
$length = 4;
$vertify_code = '';
for ($i=1;$i<=$length;$i++){
$fontsize = 6;
$x = $i*100/5;
$y = mt_rand(5,10);
$data = 'ABCDEFGHIGKMNOPQRSTUVWXYabcdefghijkmnopqrstuvwxy34567890';
$fontcontent = substr($data,mt_rand(0,strlen($data)),1);
$vertify_code .= $fontcontent;
$fontcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $vertify_code;
$pointnum = 200;
for ($i=1;$i<=200;$i++){
$x = mt_rand(1,99);
$y = mt_rand(1,29);
$pointcolor = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,$x,$y,$pointcolor);
}
$linenum = 3;
for ($i=1;$i<=$linenum;$i++){
$linecolor = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
目標6
將已生成的驗證碼提供給用戶,并校驗用戶驗證碼的正確性
TIPS
創建兩個PHP文件:vertify.php和form.php
vertify.php文件代碼
session_start();
$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image,204,204,204);
imagefill($image,0,0,$bgcolor);
$length = 4;
$vertify_code = '';
for ($i=1;$i<=$length;$i++){
$fontsize = 6;
$x = $i*100/5;
$y = mt_rand(5,10);
$data = 'ABCDEFGHIGKMNOPQRSTUVWXY34567890';
$fontcontent = substr($data,mt_rand(0,strlen($data)),1);
$vertify_code .= $fontcontent;
$fontcolor = imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $vertify_code;
$pointnum = 200;
for ($i=1;$i<=200;$i++){
$x = mt_rand(1,99);
$y = mt_rand(1,29);
$pointcolor = imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
imagesetpixel($image,$x,$y,$pointcolor);
}
$linenum = 3;
for ($i=1;$i<=$linenum;$i++){
$linecolor = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));
imageline($image,mt_rand(1,99),mt_rand(1,29),mt_rand(1,99),mt_rand(1,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
form.php文件代碼
header('content-type:text/html;charset=utf-8');
if (isset($_REQUEST['authcode'])){
session_start();
if (strtoupper($_REQUEST['authcode']) == $_SESSION['authcode']){
echo '輸入正確';
}else{
echo '輸入錯誤';
}
}
?>
確認驗證碼驗證碼圖片:
請輸入圖片中的內容:
目標7
驗證碼的動態校驗及設計原理:“看不清、換一個”的實現原理
簡單三步即可實現動態驗證
增加可點擊的“換一個”文案
用JS選取器選取驗證碼圖片
用JS修改驗證碼圖片地址(改src)
header('content-type:text/html;charset=utf-8');
if (isset($_REQUEST['authcode'])){
session_start();
if (strtoupper($_REQUEST['authcode']) == $_SESSION['authcode']){
echo '輸入正確';
}else{
echo '輸入錯誤';
}
}
?>
確認驗證碼驗證碼圖片:
換一個?
請輸入圖片中的內容:
目標8
復雜驗證碼的實現:圖片、視頻驗證碼
圖片、視頻驗證碼實現方案類似
準備一定量的驗證碼物料庫
做好物料的對應關系
總結
以上是生活随笔為你收集整理的php实现验证码正确输入_PHP实现验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unity材质球发光_Unity Lig
- 下一篇: android tv 云播放器,Andr