程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP制作圖形驗證碼代碼分享,php圖形驗證碼代碼

PHP制作圖形驗證碼代碼分享,php圖形驗證碼代碼

編輯:關於PHP編程

PHP制作圖形驗證碼代碼分享,php圖形驗證碼代碼


效果:

myvcode.class.php:封裝創建驗證碼的類

<?php
/*
* file:myvcode.class.php
* 驗證碼類,類名Vcode
*/
class Vcode
{
private $width;              /*驗證碼寬度*/
private $height;             /*驗證碼高度*/
private $codeNum;            /*驗證碼字符個數*/
private $checkCode;            /*驗證碼字符*/
private $image;                /*驗證碼資源*/
private $pixNum;            /*繪制干擾點的個數*/
private $lineNum;            /*繪制干擾線的條數*/
/*
*構造方法實例化驗證碼對象,並初始化數據
*@param int $width         設置默認寬度
*@param int $height     設置默認高度
*@param int $codeNum    設置驗證碼中的字符個數
*@param int $pixNum        設置干擾點的個數
*@param int $lineNum    設置干擾線的數量
*/
function __construct($width=80,$height=40,$codeNum=4,$pixNum=40,$lineNum=5)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->pixNum = $pixNum;
$this->lineNum = $lineNum;
}
/*內部私有方法,創建圖像資源*/
private function getCreateImage()
{
$this->image = imagecreatetruecolor($this->width, $this->height);
$white = imagecolorallocate($this->image,0xff,0xff,0xff);
imagefill($this->image, 0, 0, $white);
$black = imagecolorallocate($this->image,0,0,0);
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $black);
}
/*內部私有方法,繪制字符,去掉o0Llz和012*/
private function createCheckCode()
{
$code = '3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKMNPQRSTUVWXY';
$this->checkCode = "";
for($i=0; $i<$this->codeNum;$i++)
{
$char = $code{rand(0,strlen($code) - 1)};
$this->checkCode .= $char;
$fontColor = imagecolorallocate($this->image, rand(0,128), rand(0,128),rand(0,128));
$fontSize = rand(3,5);
$x = rand(0,$this->width-imagefontwidth($fontSize));
$y = rand(0,$this->height-imagefontheight($fontSize));
imagechar($this->image, $fontSize, $x, $y, $char, $fontColor);
}
}
/*內部私有方法設置干擾元素*/
private function setDisturbColor()
{
/*繪制干擾點*/
for($i=0; $i<$this->pixNum; $i++)
{
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($this->image, rand(1,$this->width-2), rand(1,$this->height-2), $color);
}
/*繪制干擾線*/
for($i=0; $i<$this->lineNum; $i++)
{
$color = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
imageline($this->image, rand(1,$this->width / 2), rand(1,$this->height / 2),
rand($this->width / 2,$this->width – 2), rand($this->height / 2,$this->height – 2), $color);
}
}
/*開啟session保存 利用echo 輸出圖像*/
function __toString()
{
$_SESSION['code'] = strtoupper($this->checkCode);
$this->getCreateImage();
$this->createCheckCode();
$this->setDisturbColor();
$this->outputImg();
}
/*內部私有方法輸出圖像*/
private function outputImg()
{
header("content-type:image/png");
imagepng($this->image);
}
/*析構方法,釋放對象*/
function __destruct()
{
imagedestroy($this->image);
}
}
?>

imgcode.php輸出圖像

<?php
session_start();
require_once('myvcode.class.php');
echo new Vcode();
?>

test.html:同過img標簽引用

<img src="imgcode.php">

可以加一個a標簽,用js實現換一張效果:

/*局部刷新換驗證碼*/
function changeCode()
{
var imgcode = document.getElementById(‘code');
var change = document.getElementById(‘change');
change.onclick = function()
{
/*必須加後面的參數才能刷新*/
imgcode.src='code.php?tm'+Math.random();
}
}

code和change分別是img和a的id


php的圖片驗證碼代碼

這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
<?php

session_start();

$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}

ob_clean(); //清理緩沖

if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //顯示4個字符

$imageX = strlen($radomstring)*8; //圖像的寬
$imageY = 20; //圖像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像

//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, ......余下全文>>
 

插入php圖片驗證碼後,遞交時寫判斷代碼??

action.php

<?
session_start();
$password = md5(trim($_POST['password']));
$str_reg=$_POST['number']; //用戶填寫的驗證碼
$str_reg = strtoupper($str_reg); //轉換大寫

if ($str_reg !=$_SESSION['yzm'] or empty($str_reg) )
{echo "驗證碼錯誤";
}
else
{
$_SESSION['yzm']=""; //清除session
……
執行數據庫查詢操作,驗證用戶名,密碼
}
?>
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved