ThinkPHPÑéÖ¤Âë²å¼þÔõÑù×°ÖÃ
ÎÒÃÇÊ×ÏÈÐè·¿ªThinkPHP¹ÙÍø£¬²¢ËÑË÷ÓëÑéÖ¤ÂëÏà¹ØµÄÄÚÈÝ¡£ÎÒÃÇ¿ÉÒÔÔÚËÑË÷Ч¹ûÖÐÕÒµ½Ò»Ð©ÎĵµÏÈÈÝÑéÖ¤ÂëÒÔ¼°ÒѾ¿ª·¢ºÃµÄÑéÖ¤Âë²å¼þ¡£±¾ÎĽ«ÏÈÈÝÁ½ÖÖÑéÖ¤Â뼯³É·½·¨£ºÊ¹Óùٷ½ÌṩµÄÑéÖ¤Âë²å¼þºÍÊÖ¶¯±àд´úÂë¡£
1.ʹÓùٷ½ÑéÖ¤Âë²å¼þ
ÔÚ¹Ù·½ÎĵµÖУ¬ÎÒÃÇ¿ÉÒÔÕÒµ½ThinkPHPÑéÖ¤Âë²å¼þµÄʹÓÃÒªÁ졣ʹÓùٷ½²å¼þÐèÒªÖ´ÐÐÒÔÏ°취£º
1.1ÔÚThinkPHP¿ò¼ÜµÄextendĿ¼ÏÂн¨Ò»¸öVerifyÎļþ¼Ð£¬²¢½«ÏÂÔغõÄÑéÖ¤Âë²å¼þ·ÅÈëÆäÖС£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
1.2Éó²éThinkPHPÉèÖÃÎļþ£¬½«ÑéÖ¤ÂëÉèÖÃÏîÖ¸Ïò¸Õ°²ÅÅÑéÖ¤Âë²å¼þµÄÎļþ¼Ð¡£Ïêϸ´úÂëÈçÏ£º
'verify' =>[ //ʹÓÃÖÐÎÄÑéÖ¤Âë 'useZh'=>false, //ÑéÖ¤Âë×ÖÌå¾Þϸ(px) 'fontSize'=>25, //ÑéÖ¤ÂëλÊý 'length'=>5, //ÑéÖ¤ÂëͼƬ¿í¶È(ÏñËØ) 'imageW'=>0, //ÑéÖ¤ÂëͼƬ¸ß¶È(ÏñËØ) 'imageH'=>0, //¹Ø±ÕÑéÖ¤ÂëÔÓµã 'useNoise'=>true, //Åä¾°ÑÕÉ«(16½øÖÆÉ«Öµ) 'bg'=>[243, 251, 254], //ÐèÒª°üÀ¨µÄ×Ö·ûÜöÝÍ 'codeSet'=>'0123456789', //ÑéÖ¤Âë×Ö·û¾àÀë(px) 'seKey'=>"ThinkPHP.CN_",//ÃÜÔ¿ ... ],
µÇ¼ºó¸´ÖÆ
ÐèҪעÖصÄÊÇ£¬Æ¾Ö¤ÏÖÕæÏàÐΣ¬¿ÉÒÔÉèÖòÎÊýimageWºÍimageH¡£ÈôÊDz»ÉèÖã¬ÔòĬÈϽ«ÑéÖ¤ÂëͼƬµÄ¾ÞϸÓëÊä³öͼƬµÄ¾ÞϸÏàͬ¡£
1.3ÔÚÐèÒªÊä³öÑéÖ¤ÂëµÄµØ·½£¬Ê¹ÓÃÒÔÏ´úÂë¼´¿É¼¯³É¹Ù·½ÑéÖ¤Âë²å¼þ£º
$img = ( new \Think\Verify())->entry(); echo $img;
µÇ¼ºó¸´ÖÆ
2.ÊÖ¶¯±àдÑéÖ¤ÂëÌìÉú´úÂë
³ýÁËʹÓùٷ½²å¼þ£¬ÎÒÃÇ»¹¿ÉÒÔÊÖ¶¯±àдÑéÖ¤ÂëÌìÉú´úÂë¡£ÏêϸÁ÷³ÌÈçÏ£º
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÑéÖ¤ÂëÀ࣬ÆäÖаüÀ¨ÌìÉúºÍÊä³öÑéÖ¤ÂëµÄÒªÁì¡£ÒÔÏ´úÂëΪÊÖ±àÑéÖ¤ÂëÀàµÄÖ÷Òª²¿·Ö£º
class VerifyCode { //ÑéÖ¤Âë×Ö·û³¤¶È private $length = 4; //ÑéÖ¤Âë×Ö·ûÊý×é private $codes = []; //ÑéÖ¤ÂëÌìÉú public function generate() { //ÌìÉú×Ö·ûÊý×é $this->codes = []; for($i = 0; $i < $this->length; ++$i) { $this->codes[] = chr(mt_rand(48, 57)); } //ÉúÑÄ×Ö·ûÊý×éµ½sessionÖÐ session('verifycode', implode('', $this->codes)); //¿ªÆôÊä³ö»º´æ ob_start(); header('Content-Type:/image/png'); //½¨ÉèÑéÖ¤ÂëͼƬ $image = imagecreate(100, 40); //ÉèÖû²¼Åä¾°ÑÕÉ« $bg_color = imagecolorallocate($image, 238, 238, 238); imagefill($image, 0, 0, $bg_color); //»æÖÆÑéÖ¤Âë×Ö·û for($i = 0; $i < $this->length; ++$i) { $font_file = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'; $text_color = imagecolorallocate( $image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150)); imagettftext($image, 24, mt_rand(-20, 20), 5 + $i * 25, 30, $text_color, $font_file, $this->codes[$i]); } //Êä³öÑéÖ¤ÂëͼƬ imagepng($image); imagedestroy($image); $img = ob_get_contents(); ob_end_clean(); return $img; } }
µÇ¼ºó¸´ÖÆ
2.2ʹÓÃÒÔÏ´úÂë¼´¿ÉÔÚÑéÖ¤ÐèÒªÑéÖ¤ÂëµÄµØ·½ÌìÉú²¢Êä³öÑéÖ¤Â룺
$vf = new VerifyCode(); echo $vf->generate();
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇThinkPHPÑéÖ¤Âë²å¼þÔõÑù×°ÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡