PHP
·
发表于 5年以前
·
阅读量:8301
±¾ÎÄʵÀý½²ÊoÁËphpʵÏÖµÄCaptchaÑeÖ¤ÂeÀa£¬ÔÚphp³ÌÐoÉe¼ÆÖÐÓÐ׿«Æa¹aºµÄÓ¦Óá£ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼¡£¾ßÌa½¨ÈçÏ£º
ÑeÖ¤ÂeÀaÎļþÈçÏ£º
<?php
/** Captcha ÑeÖ¤ÂeÀa
* Date: 2011-02-19
* Author: fdipzone
*/
class Captcha{ //class start
private $sname = '';
public function __construct($sname=''){ // $sname captcha session name
$this->sname = $sname==''? 'm_captcha' : $sname;
}
/** Éu³ÉÑeÖ¤ÂeͼƬ
* @param int $length ÑeÖ¤Âe³¤¶È
* @param Array $param …¢"µ
* @return IMG
*/
public function create($length=4,$param=array()){
Header("Content-type: image/PNG");
$authnum = $this->random($length); //Éu³ÉÑeÖ¤Âe×Ö*u.
$width = isset($param['width'])? $param['width'] : 13; //ÎÄ×Ö¿i¶È
$height = isset($param['height'])? $param['height'] : 18; //ÎÄ×ָ߶È
$pnum = isset($param['pnum'])? $param['pnum'] : 100; //¸ÉÈÅÏoËظoÊý
$lnum = isset($param['lnum'])? $param['lnum'] : 2; //¸ÉÈÅÏßÌoÊý
$this->captcha_session($this->sname,$authnum); //½«Ëae»uÊýдÈesession
$pw = $width*$length+10;
$ph = $height+6;
$im = imagecreate($pw,$ph); //imagecreate() н¨Í¼Ïñ£¬´oСΪ x_size ºÍ y_size µÄ¿Õ°×ͼÏñ¡£
$black = ImageColorAllocate($im, 238,238,238); //ÉeÖñ³¾°ÑÕÉ«
$values = array(
mt_rand(0,$pw), mt_rand(0,$ph),
mt_rand(0,$pw), mt_rand(0,$ph),
mt_rand(0,$pw), mt_rand(0,$ph),
mt_rand(0,$pw), mt_rand(0,$ph),
mt_rand(0,$pw), mt_rand(0,$ph),
mt_rand(0,$pw), mt_rand(0,$ph)
);
imagefilledpolygon($im, $values, 6, ImageColorAllocate($im, mt_rand(170,255),mt_rand(200,255),mt_rand(210,255))); //ÉeÖøÉÈŶa±ßÐε×ͼ
/* ÎÄ×Ö */
for ($i = 0; $i < strlen($authnum); $i++){
$font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));//ÉeÖÃÎÄ×ÖÑÕÉ«
$x = $i/$length * $pw + rand(1, 6); //ÉeÖÃËae»uX×ø±e
$y = rand(1, $ph/3); //ÉeÖÃËae»uY×ø±e
imagestring($im, mt_rand(4,6), $x, $y, substr($authnum,$i,1), $font);
}
/* ¼ÓÈe¸ÉÈÅÏoËØ */
for($i=0; $i<$pnum; $i++){
$dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //ÉeÖÃÔÓµaÑÕÉ«
imagesetpixel($im, mt_rand(0,$pw) , mt_rand(0,$ph) , $dist);
}
/* ¼ÓÈe¸ÉÈÅÏß */
for($i=0; $i<$lnum; $i++){
$dist = ImageColorAllocate($im, mt_rand(50,255),mt_rand(150,255),mt_rand(200,255)); //ÉeÖÃÏßÑÕÉ«
imageline($im,mt_rand(0,$pw),mt_rand(0,$ph),mt_rand(0,$pw),mt_rand(0,$ph),$dist);
}
ImagePNG($im); //ÒÔ PNG ¸ñʽ½«Í¼ÏñÊa³oµ½a¯ÀÀÆ÷»oÎļþ
ImageDestroy($im); //Ïu»ÙһͼÏñ
}
/** ¼i²eÑeÖ¤Âe
* @param String $captcha ÑeÖ¤Âe
* @param int $flag ÑeÖ¤³É¹¦ºo 0:²»Ça³ýsession 1:Ça³ýsession
* @return boolean
*/
public function check($captcha,$flag=1){
if(empty($captcha)){
return false;
}else{
if(strtoupper($captcha)==$this->captcha_session($this->sname)){ //¼i²aÑeÖ¤Âe
if($flag==1){
$this->captcha_session($this->sname,'');
}
return true;
}else{
return false;
}
}
}
/* ²uÉuËae»uÊýº¯Êý
* @param int $length ÐeÒªËae»uÉu³ÉµÄ×Ö*u´®"µ
* @return String
*/
private function random($length){
$hash = '';
$chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
/** ÑeÖ¤Âesession´¦Ài*½*¨
* @param String $name captcha session name
* @param String $value
* @return String
*/
private function captcha_session($name,$value=null){
if(isset($value)){
if($value!==''){
$_SESSION[$name] = $value;
}else{
unset($_SESSION[$name]);
}
}else{
return isset($_SESSION[$name])? $_SESSION[$name] : '';
}
}
} // class end
?>
demoʾÀý³ÌÐoÈçÏ£º
<?php
session_start();
require_once('Captcha.class.php');
$obj = new Captcha($sname); # ´´½¨CaptchaÀa¶ÔÏo
# $snameΪ±£´aecaptchaµÄsession name,¿ÉÁo¿Õ,Áo¿Õ„tΪ'm_captcha'
$obj->create($length,$param); #´´½¨Captcha²¢Êa³oͼƬ
# $lengthΪCaptcha³¤¶È£¬¿ÉÁo¿Õ£¬Ä¬ÈÏΪ4
/* $param = array(
'width' => 13 captcha ×Ö*u¿i¶È
'height' => 18 captcha ×Ö*u¸ß¶È
'pnum' => 100 ¸ÉÈŵa¸oÊý
'lnum' => 2 ¸ÉÈÅÏßÌoÊý
)
¿ÉÁo¿Õ
*/
$obj->check($captcha,$flag); # ¼i²eÓû§ÊaÈeµÄÑeÖ¤ÂeÊÇ*ñÕýÈ*£¬true or false
# $captchaΪÓû§ÊaÈeµÄÑeÖ¤Âe,±ØÌi
# $flag ¿ÉÁo¿Õ£¬Ä¬ÈÏΪ1
# 1:µ±ÑeÖ¤³É¹¦ºo×Ô¶¯Ça³ýcaptcha session
# 0:µ²ÑeÖ¤³É¹¦ºo²»Ça³ýcaptcha session,ÓÃÓÚajax¼i²e
?>
ÏaÐű¾ÎÄËuÊo¶Ô´o¼Òphp³ÌÐoÉe¼ÆµÄѧϰÓÐÒ»¶¨µÄ½e¼ø¼ÛÖµ¡£