php實現網站驗證碼功能

來源:互聯網
上載者:User
本篇文章主要介紹php實現網站驗證碼功能,感興趣的朋友參考下,希望對大家有所協助。

驗證碼是網站常用的一項安全措施,也是新人站長較難掌握的一項技能,這裡我向大家介紹一簡單有效驗證碼實現方法。

開始之前

在正式開始之前我們需要開啟php的gd2圖形庫支援(在php.ini,中搜尋“php_gd2.dll”,找到“;extension=php_gd2.dll”並去掉句首的分號) 。

可以參考:如何開啟php的gd2庫

核心:img.php

這個頁面產生一張驗證碼並將正確數值寫入 Session

隨機一個4位驗證碼

$check=rand(1000,9999);

將產生的驗證碼寫入session

Session_start(); $_SESSION["check"] = $check;

建立一張圖片

$im = imagecreate(80,30);

由於這種圖片的背景預設是黑色的所以我們要用白色填充。

imagefill($im,0,0,ImageColorAllocate($im, 255,255,255));

使用imageline隨機繪製兩條實線

$y1=rand(0,30); $y2=rand(0,30); $y3=rand(0,30); $y4=rand(0,30); imageline($im,0,$y1,70, $y3,000); imageline($im,0,$y2,70, $y4,000);

在隨機位置繪製文字

$strx=rand(3,15); $stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10));

輸出映像

Header("Content-type: image/PNG"); ImagePNG($img);

結束,下面是完整代碼

<?php $check=rand(1000,9999);Session_start(); $_SESSION["check"] = $check; $img = imagecreate(80,30); imagefill($img,0,0,ImageColorAllocate($img,255,255,255)); $y1=rand(0,30); $y2=rand(0,30); $y3=rand(0,30); $y4=rand(0,30); imageline($img,0,$y1,70, $y3,ImageColorAllocate($img,55,255,25)); imageline($img,0,$y2,70, $y4,ImageColorAllocate($img,55,55,255)); $strx=rand(3,15); $stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,0,1),ImageColorAllocate($img,34,87,100)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,1,1),ImageColorAllocate($img,781,117,78)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,2,1),ImageColorAllocate($img,160,40,40)); $strx+=rand(15,20);$stry=rand(2,15); imagestring($img,5,$strx,$stry,substr($check,3,1),ImageColorAllocate($img,25,55,10)); Header("Content-type: image/PNG"); ImagePNG($img);

使用者介面:index.php

想必大家都知道怎麼做,我就直接給出代碼了

 <!DOCTYPE html><html><body><form action="action.php" method="post"><input type="text" name="cikle" placeholder="驗證碼"><br><img id="cikle" style="-webkit-user-select: none" src="img.php"><input type="submit" value="Submit"></form> </body></html>

以上的代碼將使用者輸入的數值傳遞到“action.php”中

檢查:action.php

這一步要將使用者輸入數值與session中的數值進行比對

相等,輸出“正確”

不相等,輸出“不正確”

<?phpSession_start(); if ($_SERVER["REQUEST_METHOD"] == "POST") { if($_SESSION["check"]!=intval($_POST["cikle"])){ echo "不正確"; }else{ echo "正確"; }}

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.