thinkphp5ÑéÖ¤Âëʧ°Ü
thinkphp5ÊÇÒ»¿î»ùÓÚmvcģʽ¿ª·¢µÄphp¿ò¼Ü£¬¾ßÓмòÆÓÒ×Óá¢Ç¿Ê¢µÄÌص㣬ÆÕ±éÓ¦ÓÃÓÚÆóÒµ¼¶webÓ¦Óÿª·¢¡£
ÆäÖÐÑéÖ¤Â빦ЧÊǽÏÁ¿³£ÓõÄÇå¾²ÑéÖ¤¹¦Ð§Ö®Ò»£¬µ«ÔÚÏÖʵ¿ª·¢Àú³ÌÖУ¬Ðí¶àÈË»áÓöµ½ÑéÖ¤Âë²»ÉúЧ»òÕßÑé֤ʧ°ÜµÄÇéÐΡ£ÏÂÃæÎÒÃǾÍÀ´ÆÊÎöÒ»ÏÂÕâЩÇéÐεĿÉÄÜÔµ¹ÊÔÓÉÏ¢Õù¾öÒªÁì¡£
ÑéÖ¤Âë²»ÏÔʾµÄÎÊÌâ
Ê×ÏÈ£¬Ó¦¸Ã¼ì²éÊÇ·ñÒѾ׼ȷÒýÈëÁËÑéÖ¤Âë²å¼þ¡£
ÔÚThinkPHP5ÖУ¬ÑéÖ¤Âë²å¼þλÓÚthinkcaptchaĿ¼Ï£¬¿ÉÒÔͨ¹ýÈçÏ´úÂë¾ÙÐÐÒýÈ룺
use thinkcaptchaCaptcha; //ÏÔʾÑéÖ¤Âë public function verify(){ $captcha = new Captcha(); return $captcha->entry(); }
µÇ¼ºó¸´ÖÆ
ÔÚǰ̨µÄHTML´úÂëÖмÓÈëÑéÖ¤Â룺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<img src="{:captcha_src()}" alt="captcha" onclick="this.src='{:captcha_src()}?t='+Math.random();">
µÇ¼ºó¸´ÖÆ
ÈôÊÇÑéÖ¤ÂëÕÕ¾ÉÎÞ·¨Õý³£ÏÔʾ£¬Ôò¿ÉÄÜÊÇ»º´æÎÊÌ⣬¿ÉÒÔ½«ä¯ÀÀÆ÷»º´æɨ³ý»òÕßʵÑéʹÓÃÆäËûä¯ÀÀÆ÷¾ÙÐвâÊÔ¡£
ÑéÖ¤ÂëÑé֤ʧ°ÜµÄÎÊÌâ
ÈôÊÇÈ·±£ÑéÖ¤ÂëÒѾ׼ȷÏÔʾ£¬¿ÉÊÇÔÚÑé֤ʱÌáÐÑÑéÖ¤Âë¹ýʧ£¬ÄǾÍÐèÒª¼ì²éÏÂÃ漸µã£º
2.1 ÑéÖ¤ÂëÑé֤ʱÌá½»µÄ±íµ¥²ÎÊýÃû³ÆÊÇ·ñ׼ȷ
ÔÚĬÈÏÇéÐÎÏ£¬ThinkPHP5µÄÑéÖ¤Âë²å¼þ»áÌìÉúÒ»¸öÃûΪcaptchaµÄPOST²ÎÊýÀ´´æ´¢ÑéÖ¤µÄÑéÖ¤ÂëÖµ£¬ÈôÊÇÑé֤ʧ°Ü£¬ÐèÒª·µ»ØJSONÃûÌõĹýʧÐÅÏ¢¡£Òò´Ë£¬ÔÚÑé֤ʱÐèҪȷ±£±íµ¥ÖÐÌá½»µÄ²ÎÊýÃûҲΪcaptcha£¬ÀýÈ磺
//ÑéÖ¤ÑéÖ¤Âë if (!captcha_check(input('post.captcha'))) { return json([ 'status' => '0', 'msg' => 'ÑéÖ¤Âë¹ýʧ£¡' ]); }
µÇ¼ºó¸´ÖÆ
2.2 ÑéÖ¤Âë²»Çø·Ö¾ÞϸдµÄÎÊÌâ
ÑéÖ¤ÂëĬÈÏÊÇÇø·Ö¾ÞϸдµÄ£¬Òò´ËÔÚ¼ìÄ¥Á·Ö¤Âëʱ£¬ÐèҪȷ±£ÊäÈëµÄÑéÖ¤ÂëÓëÌìÉúµÄÑéÖ¤ÂëÍêÈ«Ò»Ö¡£ÈôÊÇÏëÒªÑéÖ¤Âë²»Çø·Ö¾Þϸд£¬¿ÉÒÔÔÚŲÓÃcaptcha()ÒªÁìʱÌí¼Ó²ÎÊý£¬ÀýÈ磺
$captcha = new Captcha(['useZh' => false, 'useImgBg' => true, 'fontSize' => 20, 'useNoise' => true, 'length' => 4, 'useCurve' => false, 'fontttf' => '4.ttf', 'bg' => [151, 232, 66], 'reset' => true, 'codeSet' => '0123456789', 'expire' => 300, 'zhSet' => '']);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ²ÎÊýÖУ¬useZh²ÎÊýÓÃÓÚÏÔʾÖÐÎÄÑéÖ¤Â룬useImgBgºÍuseNoise²ÎÊýÓÃÓÚÌìÉúÅ侰ͼºÍÔëµã£¬length²ÎÊýÌåÏÖÑéÖ¤ÂëµÄ³¤¶È£¬codeSet²ÎÊýÉèÖÃÑéÖ¤Âë×Ö·û¼¯£¬expire²ÎÊýÉèÖÃÑéÖ¤ÂëµÄÓâÆÚʱ¼ä¡£×¢ÖØ£¬ÔÚÕâÀï°Ñ zhSet ÉèÖÃΪ¿Õ×Ö·û´®£¬ÌåÏÖ²»ÆôÓÃÖÐÎÄÑéÖ¤Âë¡£
2.3 ÑéÖ¤ÂëºÍ±íµ¥Ìá½»ÔÚͳһҳÃæ
ÈôÊÇÑéÖ¤ÂëºÍ±íµ¥Ìá½»ÔÚͳһҳÃæÖУ¬²¢ÇÒÑéÖ¤²Ù×÷ÐèҪͨ¹ýAjaxÌá½»£¬ÄÇô¿ÉÄÜ»áÓÉÓÚ¿çÓò¡¢sessionʧЧµÈÔµ¹ÊÔÓɵ¼ÖÂÑéÖ¤ÂëÎÞ·¨ÑéÖ¤Àֳɡ£´Ëʱ£¬ÐèÒªÔÚ¿çÓòÇéÐÎÏÂÉèÖÃAccess-Control-Allow-Origin£¬ÀýÈ磺
header('Access-Control-Allow-Origin: *');
µÇ¼ºó¸´ÖÆ
»¹ÐèҪȷ±£sessionת´ï¹ýÀ´£¬¿ÉÒÔÔÚsession_start()֮ǰ¼ÓÉÏ£º
header('P3P: CP=CAO PSA OUR'); session_start();
µÇ¼ºó¸´ÖÆ
¿ÉÒÔ×ÐϸÔĶÁThinkPHP5ÊÖ²áÖеÄÓйØÑéÖ¤Âë²å¼þµÄ²¿·Ö£¬»òÕßÔÚ¹Ù·½ÂÛ̳ÖÐËÑË÷Ïà¹ØµÄÎÊÌ⣬»ñµÃ¸ü¶à¹ØÓÚ¸ÃÎÊÌâµÄ½â¾öÒªÁìºÍ¼¼ÇÉ¡£
×ÜÖ®£¬ÔÚÉè¼ÆºÍʵÏÖÑéÖ¤Âëʱ£¬ÓÐÐëÒªÔÚÇå¾²ÐÔºÍÓû§ÌåÑéÖ®¼ä¾ÙÐÐȨºâÇå¾²ºâ£¬×ñÕÕͨÓõÄÉè¼ÆÔÔòºÍ×î¼Ñʵ¼ù£¬²¢Ê¹ÓÃÒѾ¼ì²é¹ýµÄµÚÈý·½×é¼þºÍ¿â£¬ÒÔÈ·±£ÑéÖ¤ÂëµÄ¿É¿¿ÐÔºÍÓÐÓÃÐÔ¡£
ÒÔÉϾÍÊÇthinkphp5ÑéÖ¤Âëʧ°ÜµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡