PHP實現(xiàn)簡單實用的驗證碼類
更新時間:2015年07月29日 11:12:38 作者:DDIAN
這篇文章主要介紹了PHP實現(xiàn)簡單實用的驗證碼類,包含驗證碼常用的隨機驗證碼、干擾線、圖片生成與輸出等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了PHP實現(xiàn)簡單實用的驗證碼類。分享給大家供大家參考。具體如下:
<?php /** * @version 1.0 * @author bolted snail * @date 2011-10-15 * @PHP驗證碼類 * 使用方法: * $image=new Captcha(); * $image->config('寬度','高度','字符個數(shù)','驗證碼session索引'); * $image->create();//這樣就會向瀏覽器輸出一張圖片 * //所有參數(shù)都可以省略, * 默認(rèn)是:寬80 高20 字符數(shù)4 驗證碼session索引captcha_code * 第四個參數(shù)即把驗證碼存到$_SESSION['captcha_code'] * 最簡單使用示例: * $image=new Captcha(); * $image->create();//這樣就會向瀏覽器輸出一張圖片 */ class Captcha { private $width=80,$height=20,$codenum=4; public $checkcode; //產(chǎn)生的驗證碼 private $checkimage; //驗證碼圖片 private $disturbColor = ''; //干擾像素 private $session_flag='captcha_code';//存到session中的索引 //嘗試開始session function __construct(){ @session_start(); } /* * 參數(shù):(寬度,高度,字符個數(shù)) */ 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; } function create() { //輸出頭 $this->outFileHeader(); //產(chǎn)生驗證碼 $this->createCode(); //產(chǎn)生圖片 $this->createImage(); //設(shè)置干擾像素 $this->setDisturbColor(); //往圖片上寫驗證碼 $this->writeCheckCodeToImage(); imagepng($this->checkimage); imagedestroy($this->checkimage); $_SESSION[$this->session_flag]=$this->checkcode; } /* * @brief 輸出頭 */ private function outFileHeader() { header ("Content-type: image/png"); } /** * 產(chǎn)生驗證碼 */ private function createCode() { $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum)); } /** * 產(chǎn)生驗證碼圖片 */ 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); // 黑色邊框 } /** * 設(shè)置圖片的干擾像素 */ 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); } } function __destruct() { unset($this->width,$this->height,$this->codenum,$this->session_flag); } } ?>
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
php 實現(xiàn)賬號不能同時登陸的方法分析【當(dāng)其它地方登陸時,當(dāng)前賬號失效】
這篇文章主要介紹了php 實現(xiàn)賬號不能同時登陸的方法,結(jié)合實例形式分析了PHP基于session實現(xiàn)當(dāng)其它地方登陸時,當(dāng)前賬號失效的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03PHP Post獲取不到非表單數(shù)據(jù)的問題解決辦法
這篇文章主要介紹了PHP Post獲取不到非表單數(shù)據(jù)的問題的解決辦法以及代碼分享,需要的朋友參考下。2018-02-02php中g(shù)et_object_vars()在數(shù)組的實例用法
在本篇文章小編給大家整理的是一篇關(guān)于php中g(shù)et_object_vars()在數(shù)組的實例用法,對此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02關(guān)于Appserv無法打開localhost問題的解決方法
安裝了Appserv時,無法打開http://localhost或是http://127.0.0.1 下面的具體的解決方法,大家可以參考下。多注意看下端口占用問題。2009-10-10