国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

PHP實現簡單實用的驗證碼類

2024-04-25 20:40:14
字體:
來源:轉載
供稿:網友

本文實例講述了PHP實現簡單實用的驗證碼類。分享給大家供大家參考。具體如下:

<?php

/**

* @version 1.0

* @author bolted snail

* @date 2011-10-15

* @PHP驗證碼類

* 使用方法:

* $image=new Captcha();

* $image->config('寬度','高度','字符個數','驗證碼session索引');

* $image->create();//這樣就會向瀏覽器輸出一張圖片

* //所有參數都可以省略,

* 默認是:寬80 高20 字符數4 驗證碼session索引captcha_code

* 第四個參數即把驗證碼存到$_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';//存到session中的索引

//嘗試開始session

function __construct(){

@session_start();

}

/*

* 參數:(寬度,高度,字符個數)

*/

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();

//產生驗證碼

$this->createCode();

//產生圖片

$this->createImage();

//設置干擾像素

$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");

}

/**

* 產生驗證碼

*/

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);

}

}

function __destruct()

{

unset($this->width,$this->height,$this->codenum,$this->session_flag);

}

}

?>

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大同市| 鄂伦春自治旗| 酒泉市| 永新县| 正宁县| 嘉兴市| 乌什县| 绥阳县| 抚顺县| 建始县| 聂拉木县| 深泽县| 绥化市| 迁安市| 芒康县| 河东区| 洛浦县| 凤山县| 龙门县| 营口市| 阆中市| 东明县| 固阳县| 金堂县| 肇源县| 广汉市| 类乌齐县| 云南省| 巩义市| 三明市| 耒阳市| 兴山县| 镇江市| 玉环县| 德州市| 霍林郭勒市| 青阳县| 八宿县| 桃园县| 普陀区| 沂水县|