首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 實作簡單實用的驗證碼類別

     編輯:PHP 相關知識

PHP 實作簡單實用的驗證碼類別

本文將示範如何使用 PHP 建立一個簡單且實用的驗證碼類別,方便在專案中快速整合圖形驗證功能。程式碼結構清晰,支援自訂寬高、字元數量與 Session 儲存鍵名,開箱即用。

類別核心功能

  • 自動啟動 Session
  • 可設定圖片寬度、高度、驗證碼字元數
  • 內建干擾像素與隨機字體大小,提升辨識難度
  • 驗證碼自動寫入 `$_SESSION`,方便後續驗證

完整程式碼範例

<?php
/**
 * PHP 驗證碼類別
 * @version 1.0
 * @author bolted snail
 * @date 2011-10-15
 * 使用方法:
 * $image = new Captcha();
 * $image->config('寬度', '高度', '字元數', 'Session鍵名');
 * $image->create(); // 直接輸出圖片
 * 所有參數皆可省略,預設:寬80 高20 字元數4 Session鍵名 captcha_code
 * 最簡單用法:
 * $image = new Captcha();
 * $image->create();
 */
class Captcha {
    private $width = 80, $height = 20, $codenum = 4;
    public $checkcode;
    private $checkimage;
    private $disturbColor = '';
    private $session_flag = 'captcha_code';

    public function __construct() {
        @session_start();
    }

    public function config($width = '80', $height = '20', $codenum = '4', $session_flag = 'captcha_code') {
        $this->width = $width;
        $this->height = $height;
        $this->codenum = $codenum;
        $this->session_flag = $session_flag;
    }

    public function create() {
        $this->outFileHeader();
        $this->createCode();
        $this->createImage();
        $this->setDisturbColor();
        $this->writeCheckCodeToImage();
        imagepng($this->checkimage);
        imagedestroy($this->checkimage);
        $_SESSION[$this->session_flag] = $this->checkcode;
    }

    private function outFileHeader() {
        header("Content-type: image/png");
    }

    private function createCode() {
        $this->checkcode = strtoupper(substr(md5(rand()), 0, $this->codenum));
    }

    private function createImage() {
        $this->checkimage = @imagecreate($this->width, $this->height);
        $back = imagecolorallocate($this->checkimage, 255, 255, 255);
        $border = imagecolorallocate($this->checkimage, 0, 0, 0);
        imagefilledrectangle($this->checkimage, 0, 0, $this->width - 1, $this->height - 1, $back);
        imagerectangle($this->checkimage, 0, 0, $this->width - 1, $this->height - 1, $border);
    }

    private function setDisturbColor() {
        for ($i = 0; $i <= 200; $i++) {
            $this->disturbColor = imagecolorallocate($this->checkimage, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($this->checkimage, rand(2, 128), rand(2, 38), $this->disturbColor);
        }
    }

    private function writeCheckCodeToImage() {
        for ($i = 0; $i < $this->codenum; $i++) {
            $bg_color = imagecolorallocate($this->checkimage, rand(0, 255), rand(0, 128), rand(0, 255));
            $x = floor($this->width / $this->codenum) * $i;
            $y = rand(0, $this->height - 15);
            imagechar($this->checkimage, rand(5, 8), $x + 5, $y, $this->checkcode[$i], $bg_color);
        }
    }

    public function __destruct() {
        unset($this->width, $this->height, $this->codenum, $this->session_flag);
    }
}
?>

使用說明

將上述類別儲存為 Captcha.php,在需要顯示驗證碼的頁面引入並實例化:

<?php
require_once 'Captcha.php';
$captcha = new Captcha();
// 可選:自訂參數 $captcha->config(120, 40, 5, 'my_captcha');
$captcha->create();
?>

前端表單中加入 <img src="captcha.php" /> 即可顯示驗證碼圖片,提交時將使用者輸入與 `$_SESSION['captcha_code']` 比對驗證。

希望本範例能為您的 PHP 專案帶來便利。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved