thinkphpÔõôʵÏÖÇ°ºó¶ËÊèÉ¢ÑéÖ¤Âë
Ò»¡¢ÑéÖ¤ÂëµÄ×÷ÓÃ
ÔÚ»¥ÁªÍøʱ´ú£¬ÎÒÃǾ³£Ê¹ÓÃÑéÖ¤ÂëÀ´ÔöÇ¿Çå¾²ÐÔ¡£¶øʵÏÖÑéÖ¤Â빦Ч£¬¿ÉÒÔ×ÊÖúÎÒÃÇ£º
±ÜÃâ»úеÈ˹¥»÷£ºÑéÖ¤Âë¿ÉÒÔ¼ì²âÊÇ·ñÊÇÈËΪ²Ù×÷£¬ÒÔïÔ̶ñÒâ»úеÈ˺ͺڿ͵Ĺ¥»÷¡£
Ìá¸ßÇå¾²ÐÔ£ºÑéÖ¤Âë¿ÉÒÔÔöǿȨÏÞ¿ØÖÆ£¬È·±£Óû§Éí·ÝµÄÕæʵÐÔ£¬²¢±£»¤Ð§ÀÍÆ÷ºÍÍøÕ¾ÃâÊܲ»ÐëÒªµÄ¹¥»÷¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ìá¸ßÓû§ÌåÑ飺ÑéÖ¤Âë¿ÉÒÔÓÐÓÃ×èÖ¹ÁËÓû§ÓÉÓÚÒ»Á¬ÄÑÒÔʶ±ðµÄ×Ö·û¶øʧȥÐËȤ¡£
¶þ¡¢Ç°¶ËµÄÑéÖ¤ÂëʵÏÖ
ÔÚÇ°¶ËʵÏÖÑéÖ¤ÂëµÄÀú³ÌÖУ¬ÎÒÃÇÐèÒªÒÔÏÂÖ÷ÒªµÄ°ì·¨£º
È·¶¨ÑéÖ¤ÂëµÄÀàÐÍ£ºÑéÖ¤Âëͨ³£·ÖΪ×Ö·ûÑéÖ¤ÂëºÍͼÐÎÑéÖ¤Âë¡£ÔÚÉè¼ÆʱҪ˼Á¿Óû§ÌåÑéºÍÇå¾²ÐÔ¡£
ÔÚÇ°¶ËÒ³ÃæÉÏÌìÉúÑéÖ¤Â룺ʹÓÃCanvas»òÆäËûÊÖÒÕÔÚÇ°¶ËÒ³ÃæÉÏÌìÉúÑéÖ¤Âë¡£ÓÃHTML5µÄCanvasÔªÏòÀ´¶¨ÖÆÑéÖ¤ÂëµÄ×ÖÌå¡¢³ß´çºÍÑÕÉ«µÈÊÇ¿ÉÐеġ£
Ò»Ñùƽ³£ÇéÐÎÏ£¬ÎÒÃÇÐèÒªÑéÖ¤Óû§ÊäÈëÓëЧÀÍÆ÷ÌìÉúµÄÑéÖ¤ÂëÊÇ·ñÆ¥Å䡣ͨ¹ýJavaScriptºÍAjaxÊÖÒÕ£¬ÎÒÃÇÄܹ»»ñÈ¡Óû§ÊäÈë²¢½«Æä·¢Ë͵½Ð§ÀÍÆ÷¶Ë¡£
ÑéÖ¤ÑéÖ¤Â룺ÔÚЧÀÍÆ÷¶Ë¶ÔÓû§ÊäÈë¾ÙÐÐÑéÖ¤£¬ÈçÌṩһ¸öAPI½Ó¿Ú£¬½Ó¿Ú»á·µ»ØÑéÖ¤Àֳɻòʧ°ÜµÈÐÅÏ¢¸ø¿Í»§¶Ë¡£
ͨ¹ýʹÓÃÕâЩÊÖÒÕ£¬Óû§¿ÉÒÔÔÚǰ̨»ñµÃÑéÖ¤Â룬ÒÔ×èÖ¹×Ô¶¯»¯¶ñÒâ»á¼û»ò¹¥»÷¡£
Èý¡¢ºó¶ËµÄÑéÖ¤ÂëʵÏÖ
ÔÚthinkphpÖÐʵÏÖÑéÖ¤Â룬ÎÒÃÇͨ³£Òª¹Ø×¢ÒÔϼ¸¸ö·½Ã棺
½¨ÉèÒ»¸öÑéÖ¤Âë¿ØÖÆÆ÷
¿ØÖÆÑéÖ¤ÂëµÄÌìÉúºÍÑéÖ¤µÈ²Ù×÷£¬¿ÉÒÔ°ÑÑéÖ¤Âë¿ØÖÆÆ÷ÖÃÓÚºǫ́Ŀ¼Ï¡£ÔÚ¿ØÖÆÆ÷ÖУ¬Í¨³£»á°üÀ¨ÒÔϼ¸¸öÒªÁ죺
generateCode£ºÌìÉúÑéÖ¤Â룬²¢½«ÑéÖ¤Âë´æ´¢ÔÚSessionÖС£
verifyCode£ºÑéÖ¤Óû§ÊäÈëµÄÑéÖ¤ÂëÊÇ·ñ׼ȷ¡£
getCode£º·µ»ØSessionÖд洢µÄÑéÖ¤Âë¡£
ÌìÉúÑéÖ¤Âë
ÔÚÌìÉúÑéÖ¤Âëʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃGD¿âÀ´ÌìÉúͼÏñ£¬È»ºóͨ¹ýÊä³öͼÏñºÍÉúÑÄͼÏñµÄ·½·¨£¬½«ÑéÖ¤ÂëµÄЧ¹û·¢Ë͵½¿Í»§¶Ë¡£ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룺
public function generateCode($width=80,$height=22,$verifyName=''){ //ÌìÉúÒ»¸ö4λµÄËæ»ú×Ö·û´® $code = ''; $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; for($i=0;$i<4;$i++){ $code .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } //½«ÑéÖ¤Âë´æ´¢µ½sessionÖÐ if($verifyName){ session($verifyName, $code); }else{ session('verify_code', $code); } //ÌìÉúÑéÖ¤ÂëͼÏñ $img = imagecreate($width,$height); //Åä¾°Ö imagecolorallocate($img, 102,102,102); //×ÖÌåÑÕÉ« $color = imagecolorallocate($img, 255, 255, 255); //ÌìÉú×ÌÈÅÏß for($i=0;$i<5;$i++){ imageline($img,mt_rand(0,$width/2),mt_rand(0,$height/2),mt_rand($width/2,$width),mt_rand($height/2,$height),$color); } //½«ÑéÖ¤Âë»æÖƵ½Í¼ÏñÉÏ imagefttext($img, 18, 0, 10, $height-5, $color, './arial.ttf', $code); //Êä³öͼÏñ header('Pragma:no-cache'); header('Cache-Control:no-cache'); header("content-type:image/png"); imagepng($img); imagedestroy($img); }
µÇ¼ºó¸´ÖÆ
ÑéÖ¤ÑéÖ¤Âë
ÎÒÃÇÒ»Ñùƽ³£»á»ñÈ¡Óû§ÊäÈëµÄÑéÖ¤Â룬ȻºóÔڻỰÖвéÕÒ¶ÔÓ¦µÄÑéÖ¤ÂëÖµÀ´¾ÙÐÐÑéÖ¤¡£µ±»á»°´æ´¢µÄÑéÖ¤ÂëÖµÓëÓû§ÊäÈëµÄÖµÏàͬʱ£¬¾ÍÅú×¢ÑéÖ¤ÂëÑéÖ¤Àֳɡ£
// ÑéÖ¤ÂëÑéÖ¤ if(empty($verify)) { $this->error('ÑéÖ¤Âë²»¿ÉΪ¿Õ£¡'); } if($verify != session('verify_code')){ $this->error("ÑéÖ¤Âë¹ýʧ£¡"); }
µÇ¼ºó¸´ÖÆ
ËÄ¡¢Ç°ºó¶ËÊèÉ¢ÑéÖ¤ÂëʵÏÖµÄÓŵã
Ç°ºó¶ËÊèÉ¢µÄ·½·¨£¬Èúó¶Ë¿ª·¢Ö°Ô±¿ÉÒÔרעÓÚÊý¾Ý´¦Öóͷ£ºÍÂß¼ÓªÒµ£¬Ç°¶Ë¿ª·¢Ö°Ô±¿ÉÒÔרעÓÚÓû§ÌåÑéºÍ½»»¥·½·¨µÄ¿ª·¢¡£Ê¹ÓÃÇ°ºó¶ËÊèÉ¢£¬¿ÉÌá¸ßÍøÕ¾ºÍWebÓ¦ÓóÌÐòµÄÇå¾²ÐÔ£¬²¢Ó¦ÓÃÑéÖ¤ÂëÒÔÓÐÓÃ×èÖ¹¶ñÒâ×Ô¶¯»¯»á¼ûºÍ¹¥»÷¡£
ÒÔÉϾÍÊÇthinkphpÔõôʵÏÖÇ°ºó¶ËÊèÉ¢ÑéÖ¤ÂëµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡