php生成验证码类代码_PHP教程
php教程生成验证码类代码
*/
class securecode
{
private static $instance=null;
private $code = '';
private $fontfile;
private $validate;
private $image;
private $specialadd = 'special string for securecode';
private $codeexpire=86400;
private $codecookiename='secure_code';
/**
* 构造方法
*/
private function securecode()
{
$this->fontfile = dirname( __file__ ) . '/arial.ttf';
}
private function __construct()
{
$this->securecode();
}
public static function getinstance()
{
if (self::$instance==null)
self::$instance=new self();
return self::$instance;
}
/**
* 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
* @param $fontfile 文件路径
* @return void
*/
function loadfont($fontfile)
{
$this->fontfile = $fontfile;
}
/**
* 图片输出方法,在执行本方法前程序不应该有任何形式的输出
* @return void;
*/
function stroke()
{
$this->savecode();
self::sendheader();
imagegif( $this->validate );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 图片保存方法
* @param $filename 保存路径
* @return void
*/
function save($filename)
{
$this->savecode();
imagegif( $this->validate , $filename );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 验证码验证方法
* @param $input 要验证的字符串,即用户的输入内容
* @return boolean 验证结果
*/
function verify($input)
{
$input=strtolower($input);
$targetcode=$this->authcode($input);
$code=$this->getcookie();
if (empty($code)||$code!=$targetcode)
$result= false;
else
$result=true;
$_cookie[$this->codecookiename]='';
setcookie ( $this->codecookiename, '', - 1 );
return $result;
}
/**
* 图片创建方法
* @return void;
*/
function createimage()
{
$this->randcode();
$size = 30;
$width = 90;
$height = 35;
$degrees = array (
rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
);
for ($i = 0; $i
{
if (rand() % 2);
else $degrees[$i] = -$degrees[$i];
}
$this->image = imagecreatetruecolor( $size , $size );
$this->validate = imagecreatetruecolor( $width , $height );
$back = imagecolorallocate( $this->image , 255 , 255 , 255 );
$border = imagecolorallocate( $this->image , 0 , 0 , 0 );
imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
for ($i = 0; $i
{
$temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
if ($temp[2] > 60) $temp[2] = 60;
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
}
for ($i = 0; $i
{
$randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
}
$temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
if ($temp[2]
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
putenv( 'gdfontpath=' . realpath( '.' ) );
// name the font to be used (note the lack of the .ttf extension
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
$this->image = imagerotate( $this->image , $degrees[0] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
$this->image = imagerotate( $this->image , $degrees[1] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
$this->image = imagerotate( $this->image , $degrees[2] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
$this->image = imagerotate( $this->image , $degrees[3] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
}
/**
* 获取随机生成的验证码
* @return string 随机验证码,返回的验证码不进行任何处理
*/
function getcode()
{
return $this->code;
}
/**
* 生成随机码方法
* @return void;
*/
protected function randcode()
{
$alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
$randstr = array (
$alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
);
$this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
}
/**
* rgb色到hsv色转变方法
* @param $r
* @param $g
* @param $b
* @return array hsv数组
*/
protected static function rgbtohsv($r, $g, $b)
{
$tmp = min( $r , $g );
$min = min( $tmp , $b );
$tmp = max( $r , $g );
$max = max( $tmp , $b );
$v = $max;
$delta = $max - $min;
if ($max != 0) $s = $delta / $max; // s
else
{
$s = 0;
//$h = undefinedcolor;
return;
}
if ($r == $max) $h = ($g - $b) / $delta; // between yellow & magenta
else if ($g == $max) $h = 2 + ($b - $r) / $delta; // between cyan & yellow
else $h = 4 + ($r - $g) / $delta; // between magenta & cyan
$h *= 60; // degrees
if ($h
return array (
$h, $s, $v
);
}
/**
* 同上一方法功能相反
* @param $h
* @param $s
* @param $v
* @return array rgb数组
*/
protected static function hsvtorgb($h, $s, $v)
{
if ($s == 0)
{
// achromatic (grey)
$r = $g = $b = $v;
return;
}
$h /= 60; // sector 0 to 5
$i = floor( $h );
$f = $h - $i; // factorial part of h
$p = $v * (1 - $s);
$q = $v * (1 - $s * $f);
$t = $v * (1 - $s * (1 - $f));
switch ($i)
{
case 0 :
$r = $v;
$g = $t;
$b = $p;
break;
case 1 :
$r = $q;
$g = $v;
$b = $p;
break;
case 2 :
$r = $p;
$g = $v;
$b = $t;
break;
case 3 :
$r = $p;
$g = $q;
$b = $v;
break;
case 4 :
$r = $t;
$g = $p;
$b = $v;
break;
default : // case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
return array (
$r, $g, $b
);
}
/**
* 使用cookie保存验证码的方法
* @return void
*/
protected function savecode()
{
$code = $this->authcode($this->code);
$this->setcookie($code);
}
/**
* 验证码cookie值获取方法
* @return string cookie值
*/
protected function getcookie()
{
if (empty( $_cookie[$this->codecookiename] ))
{
return '';
}
else
{
return addslashes($_cookie[$this->codecookiename]);
}
}
/**
* 验证码cookie创建方法
* @param string $code 要保存的验证码
* @return void
*/
protected function setcookie($code)
{
$expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
setcookie( $this->codecookiename , $code, $expire );
}
/**
* 验证码加密方法
* @param string $code 要加密的随机码
* @return mixed 执行结果
*/
protected function authcode($code)
{
return md5($code.$this->specialadd);
}
/**
* 干扰线生成方法
* @param resource $image 图片资源句柄
* @param string $color 干扰线颜色
*/
protected static function imagelinethick($image, $color)
{
$k = rand( 5 , 20 );
for ($px = 0; $px
{
$y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
for ($i = 0; $i
{
imagesetpixel( $image , $px , $y + 10 + $i , $color );
}
}
}
/**
* http标头设置方法
* @return void
*/
protected static function sendheader()
{
header( "pragma: no-cache" );
header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" );
header( 'content-type: image/gif' );
}
}
http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22330.html

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP는 오픈 소스 MVC 프레임워크입니다. 이를 통해 애플리케이션 개발, 배포 및 유지 관리가 훨씬 쉬워집니다. CakePHP에는 가장 일반적인 작업의 과부하를 줄이기 위한 여러 라이브러리가 있습니다.

이 튜토리얼은 PHP를 사용하여 XML 문서를 효율적으로 처리하는 방법을 보여줍니다. XML (Extensible Markup Language)은 인간의 가독성과 기계 구문 분석을 위해 설계된 다목적 텍스트 기반 마크 업 언어입니다. 일반적으로 데이터 저장 AN에 사용됩니다
