您好,登錄后才能下訂單哦!
這篇文章主要介紹怎么使用php實(shí)現(xiàn)驗(yàn)證碼,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
php實(shí)現(xiàn)驗(yàn)證碼的方法:首先創(chuàng)建繪制驗(yàn)證碼類(lèi),代碼如“class Captcha {...}”;然后繪制圖片頁(yè)面;接著設(shè)置表單頁(yè)面;最后創(chuàng)建好驗(yàn)證頁(yè)面即可。
本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版,DELL G3電腦
PHP 實(shí)現(xiàn)驗(yàn)證碼
繪制驗(yàn)證碼類(lèi)
<?php class Captcha { const CODE_LENGTH = 4; // 驗(yàn)證碼長(zhǎng)度固定為 4,可以根據(jù)實(shí)際需要修改 const LINE_COUNT = 4; // 干擾線個(gè)數(shù) const DOT_COUNT = 200; // 干擾點(diǎn)個(gè)數(shù) // 驗(yàn)證碼備選字符 static $CANDIDATES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public function __construct($width, $height) { $this->width = $width; $this->height = $height; // 創(chuàng)建圖像對(duì)象 $this->image = imagecreatetruecolor($width, $height); // 創(chuàng)建驗(yàn)證碼 $this->generateCaptchaCode(); } public function __destruct() { // 銷(xiāo)毀圖像對(duì)象 imagedestroy($this->image); } public function paint() { // 繪制背景 $this->paintBackground(); // 繪制驗(yàn)證碼 $this->paintText(); // 繪制干擾 $this->paintDirty(); } public function output() { // 設(shè)置頭部為PNG圖片 header('Content-Type: image/png'); // 輸出到瀏覽器 imagepng($this->image); } public function code() { return $this->code; } private function paintBackground() { // 背景顏色設(shè)置為白色 $color = imagecolorallocate($this->image, 255, 255, 255); // 填充背景 imagefill($this->image, 0, 0, $color); } private function paintText() { // 遍歷驗(yàn)證碼,一個(gè)字符一個(gè)字符地繪制 for ($i = 0; $i < strlen($this->code); ++$i) { $fontsize = 6; $color = imagecolorallocate($this->image, rand(0,50), rand(0,50), rand(0,50)); $x = ($i * 100 / self::CODE_LENGTH) + rand(5, 10); $y = rand(5, 10); imagestring($this->image, $fontsize, $x, $y, $this->code[$i], $color); } } private function paintDirty() { // 繪制點(diǎn) for ($i = 0; $i < self::DOT_COUNT; ++$i) { // 點(diǎn)的顏色 $pointcolor = imagecolorallocate($this->image, rand(100,200), rand(100,200), rand(100,200)); // 畫(huà)點(diǎn) imagesetpixel($this->image, rand(1,99), rand(1,29), $pointcolor); } // 繪制線條 for ($i = 0; $i < self::LINE_COUNT; $i++) { // 線的顏色 $linecolor = imagecolorallocate($this->image, rand(100,200), rand(100,200), rand(100,200)); // 畫(huà)線 imageline($this->image, rand(1,$this->width-1), rand(1,29), rand(1,99), rand(1,29), $linecolor); } } private function generateCaptchaCode() { // 從備選字符串中隨機(jī)選取字符 for ($i = 0; $i < self::CODE_LENGTH; ++$i) { $len = strlen(self::$CANDIDATES); $pos = rand(0, $len - 1); $ch = self::$CANDIDATES[$pos]; $this->code .= $ch; } } private $image = NULL; // 圖像對(duì)象 private $code = ""; // 驗(yàn)證碼 private $width = 0; // 圖像長(zhǎng)度 private $height = 0; // 圖像寬度 } ?>
繪制圖片頁(yè)面
<?php session_start(); // 開(kāi)啟 Session,必須是第一句 require_once "./Captcha.php"; $captcha = new Captcha(100, 30); // 創(chuàng)建對(duì)象 $_SESSION['captcha'] = $captcha->code(); // 將驗(yàn)證碼存入Session $captcha->paint(); // 繪制 $captcha->output(); // 輸出 ?>
》
表單頁(yè)面
<?php session_start(); // 開(kāi)啟Session,必須是第一句 ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>提交頁(yè)面</title> <script> function validate_form(form) { with (form) { if (captcha.value == null || captcha.value == "") { alert("驗(yàn)證碼不能為空!"); return false; } } return true; } </script> </head> <body> <form method="post" action="./validate.php" onsubmit="return validate_form(this)"> 驗(yàn)證碼:<input type="text" name="captcha" value="" size=10> <img title="點(diǎn)擊刷新" id="captchaImg" border="1" src="./captchaImage.php" onclick="this.src='./captchaImage.php?r=' + Math.random();"></img><br> <input type="submit"/> </form> </body> </html>
驗(yàn)證頁(yè)面
<?php session_start(); // 開(kāi)啟Session,必須是第一句 ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>驗(yàn)證頁(yè)面</title> </head> <body> <?php if (isset($_POST['captcha']) && strcasecmp($_POST['captcha'], $_SESSION['captcha']) == 0) { echo "Succeeded!"; } else { ec` o "Failed!"; } ?> </body> </html>
以上是“怎么使用php實(shí)現(xiàn)驗(yàn)證碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。