驗證碼的類及使用

2022-08-02 17:54:10 字數 2597 閱讀 9882

<?php

session_start();

ini_set('display_errors', 'off');

class ccheckcodefile

//驗證碼位數 www.2cto.com

var $mcheckcodenum  = 4;

//產生的驗證碼

var $mcheckcode   = '';

//驗證碼的

var $mcheckimage  = '';

//干擾畫素

var $mdisturbcolor  = '';

//驗證碼的寬度

var $mcheckimagewidth = '80';

//驗證碼的寬度

var $mcheckimageheight  = '20';

//輸出頭

function outfileheader()

header ("content-type: image/png");

//產生驗證碼

function createcheckcode()

//$this->mcheckcode = strtoupper(substr(md5(rand()),0,$this->mcheckcodenum));

$this->mcheckcode = strtoupper(substr(rand(0,9999999999),0,$this->mcheckcodenum));

session_cache_expire(60);

session_start();

$_session["code_str"] = $this->mcheckcode;

return $this->mcheckcode;

//產生驗證碼

function createimage()

$this->mcheckimage = @imagecreate ($this->mcheckimagewidth,$this->mcheckimageheight);

imagecolorallocate ($this->mcheckimage, 255, 255, 255);

return $this->mcheckimage;

//設定的干擾畫素

function setdisturbcolor()

for ($i=0;$i<=128;$i++)

$this->mdisturbcolor = imagecolorallocate ($this->mcheckimage, rand(0,255), rand(0,255), rand(0,255));

imagesetpixel($this->mcheckimage,rand(1,100),rand(1,100),$this->mdisturbcolor);

//設定驗證碼的大小\寬\高

function setcheckimagewh($width,$height)

if($width==''||$height=='')return false;

$this->mcheckimagewidth  = $width;

$this->mcheckimageheight = $height;

return true;

//在驗證碼上逐個畫上驗證碼

function writecheckcodetoimage()

for ($i=0;$i<=$this->mcheckcodenum;$i++)

$bg_color = imagecolorallocate ($this->mcheckimage, rand(0,255), rand(0,255), rand(0,255));

$x = floor($this->mcheckimagewidth/$this->mcheckcodenum)*$i;

$y = rand(0,$this->mcheckimageheight-15);

imagechar ($this->mcheckimage, 5, $x, $y, $this->mcheckcode[$i], $bg_color);

//輸出驗證碼

function outcheckimage()

$this ->outfileheader();

$this ->createcheckcode();

$this ->createimage();

$this ->setdisturbcolor();

$this ->writecheckcodetoimage();

imagepng($this->mcheckimage);

imagedestroy($this->mcheckimage);

$c_check_code_image = new ccheckcodefile();

//$c_check_code_image ->setcheckimagewh(100,50);//設定顯示驗證碼的尺寸

$c_check_code_image->outcheckimage();

使用方法:

驗證碼驗證是否正確

session_start();

if($_post["number"] == $_session["code_str"])else{

echo "

PHP驗證碼類

php驗證碼類 如果不適用指定的字型,那麼就用imagestring 函式,如果需要遇到指定的字型,就要用到imagettftext 函式。字型的位置在c盤下windows fonts.verificationcode.class.php class verificationcode 生成驗證碼 p...

GD 驗證碼類

1 建立畫布 2 干擾 3 文字 4 輸出 5 釋放資源 class code 定有出口程式 function printimage 1 建立畫布 背景色淺色 顏色隨機 private function bg 2 干擾 private function disturb 10條隨機出現的線 for i...

PHP 驗證碼類

呼叫方法 captcha new captcha captcha createcaptcha created by phpstorm.user ming date 2018 7 15 time 下午4 34 class captcha createcode return string 生成的驗證碼字...