php怎麼實現數位驗證碼

2021-10-18 10:00:18

php實現數位驗證碼的方法:1、通過imagecreatetruecolor函數實現驗證碼底圖;2、通過imagecolorallocate方法實現數位驗證碼;3、增加干擾元素;4、儲存驗證資訊即可。

本文操作環境:windows7系統、PHP7.1版、DELL G3電腦

php怎麼實現數位驗證碼?

php實現數位驗證碼

用php實現驗證碼,驗證碼是為了區別機器與人的操作,提高安全性。需要安裝伺服器軟體,我使用的是wamp,之後編寫php驗證碼頁面。

分為以下幾個步驟來實現:

一、實現驗證碼底圖
目標:通過php程式碼,生成一張100*30px大小的圖片
方法:

resource imagecreatetruecolor(int $width,int $height)

注意事項:
(a)依賴GD擴充套件
(b)輸出圖片前,必須提前輸出圖片header資訊
(c)該方法預設輸出為黑色背景
//
二、實現數位驗證碼
方法:

int imagecolorallocate(resource $image,int $red,int $green,int $blue);
bool imagestring(resource $image,int $font,int $x,int $y,string $s,int $col);
注意事項:/控制好字型大小與分佈,避免字型重疊或顯示不全
//
三、增加干擾元素
目標:為驗證碼增加干擾元素,干擾的點或線
方法:

bool imagesetpixel(resource $image,int $x,int $y,int $color);
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)

注意事項:干擾資訊一定控制好顏色,避免「喧賓奪主」;

四、通過SESSION儲存驗證資訊
目標:在伺服器端記錄驗證碼資訊,便於使用者輸入後做校驗
方法:bool session_start(void)
注意事項:(a)session_start()必須處於指令碼最頂部
(b)多伺服器情況,需要考慮集中管理session資訊

五、驗證碼通過表單提交、校驗
目標:將已生成的驗證碼提供給使用者,並校驗使用者驗證碼的正確性
方法:html<form>表單基礎
validate.php

<?php
session_start();
$image=imagecreatetruecolor(100,30);
$bgcolor=imagecolorallocate($image,255,255,255); //#fff
imagefill($image, 0, 0, $bgcolor);
$captch_code='';
//畫出4個隨機的數位或者字母
for($i=0;$i<4;$i++){
$fontsize=6;
//為了讓數位的顏色不同,使用隨機顏色rand(0,120),120之前是深色
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
$data='abcdefghijklmnopqrstuvwxyz123456789';//由於o和0對於一些人不太好區別,所以把0去掉了
$fontcontent=substr($data,rand(0,strlen($data)),1);
$captch_code.=$fontcontent;
$x=($i*100/4)+rand(5,10);
$y=rand(5,10);
//水平畫一條字串
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode']=$captch_code;
//增加點干擾元素
for($i=0;$i<200;$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
//畫一個單一畫素
imagesetpixel($image,rand(1,99),rand(1,99),$pointcolor);
}
//增加線干擾元素
for($i=0;$i<3;$i++){
$linecolor=imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);

form.php

<?php
if(isset($_REQUEST['authcode'])){
session_start();
if(strtolower($_REQUEST['authcode'])==$_SESSION['authcode']){
echo '<font color="#0000cc">輸入正確</font>';
}else{
echo '<font color="#cc0000><b>輸入錯誤</b></font>';
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>確認驗證碼</title>
</head>
<body>
    <form method="post" action='./form.php'>
        <div>驗證碼圖片:
        <img id="code_img" border="1" src="./validate.php?r=<?php echo rand();?>" width="100" alt="">
        <a href="javascript:void(0)" οnclick="document.getElementById('code_img').src='./validate.php?r='+Math.random()">換一個?</a>
        </div>
        <div>請輸入圖片中的內容:<input type="text" name="authcode" value=""></div>
        <div><input type="submit" value="提交" style="padding:6px 20px;"></div>
    </form>
</body>
</html>

推薦學習:《》

以上就是php怎麼實現數位驗證碼的詳細內容,更多請關注TW511.COM其它相關文章!