php產生驗證碼與驗證碼驗證完整執行個體_PHP教程

來源:互聯網
上載者:User
本文章分享一個自己使用的驗證碼執行個體,從產生圖片驗證碼到利用使用驗證碼的執行個體,有需要學習的同學可以參考一下本文章哦。

hyml頁面

代碼如下 複製代碼





無標題文檔




verifycode.php檔案代碼如下

代碼如下 複製代碼
/*
圖片驗證碼 Powered By KASON
*/
session_start();
$num=4;//驗證碼個數
$width=80;//驗證碼寬度
$height=20;//驗證碼高度
$code=' ';
for($i=0;$i<$num;$i++)//產生驗證碼
{
switch(rand(0,2))
{
case 0:$code[$i]=chr(rand(48,57));break;//數字
case 1:$code[$i]=chr(rand(65,90));break;//大寫字母
case 2:$code[$i]=chr(rand(97,122));break;//小寫字母
}
}
$_SESSION["VerifyCode"]=$code;
$image=imagecreate($width,$height);
imagecolorallocate($image,255,255,255);
for($i=0;$i<80;$i++)//產生幹擾像素
{
$dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
}
for($i=0;$i<$num;$i++)//列印字元到映像
{
$char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);
}
header("Content-type:image/png");
imagepng($image);//輸出映像到瀏覽器
imagedestroy($image);//釋放資源
?>


checkcode.php檔案如下

代碼如下 複製代碼


ini_set('display_errors', 'Off');
session_start();
if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
print("驗證碼正確,");
}else{
print("驗證碼錯誤,");
}
echo "提交的驗證碼:".strtoupper($_POST["code"]).",正確的驗證碼:".strtoupper($_SESSION["VerifyCode"]);
?>

http://www.bkjia.com/PHPjc/631291.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631291.htmlTechArticle本文章分享一個自己使用的驗證碼執行個體,從產生圖片驗證碼到利用使用驗證碼的執行個體,有需要學習的同學可以參考一下本文章哦。 hyml頁面...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.