{"id":23869,"date":"2024-11-21T08:33:49","date_gmt":"2024-11-21T00:33:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23869\/"},"modified":"2024-11-21T08:33:49","modified_gmt":"2024-11-21T00:33:49","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%aa%8c%e8%af%81%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23869\/","title":{"rendered":"\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u9a8c\u8bc1\u7801"},"content":{"rendered":"<p>\u968f\u7740\u7f51\u7edc\u5b89\u5168\u7684\u65e5\u76ca\u91cd\u89c6\uff0c\u9a8c\u8bc1\u7801\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u9a8c\u8bc1\u65b9\u5f0f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u9a8c\u8bc1\u7801\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62\u6076\u610f\u653b\u51fb\u548c\u673a\u5668\u4eba\u81ea\u52a8\u5316\u64cd\u4f5c\uff0c\u4fdd\u969c\u4fe1\u606f\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u30026\u4f5c\u4e3a\u4e00\u6b3e\u4f18\u79c0\u7684php\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86\u9a8c\u8bc1\u7801\u529f\u80fd\u7684\u652f\u6301\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528\u9a8c\u8bc1\u7801\u3002<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5\u9a8c\u8bc1\u7801\u6269\u5c55<br \/>ThinkPHP6\u9ed8\u8ba4\u5e76\u4e0d\u5305\u542b\u9a8c\u8bc1\u7801\u6269\u5c55\uff0c\u9700\u8981\u624b\u52a8\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7composer\u8fdb\u884c\u5b89\u88c5\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre>composer require topthink\/think-captcha<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u914d\u7f6e\u9a8c\u8bc1\u7801<br \/>\u5728ThinkPHP6\u4e2d\uff0c\u9a8c\u8bc1\u7801\u7684\u914d\u7f6e\u9700\u8981\u5728config\u76ee\u5f55\u4e0b\u7684captcha.php\u6587\u4ef6\u4e2d\u8fdb\u884c\u3002\u8be5\u6587\u4ef6\u9ed8\u8ba4\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u624b\u52a8\u521b\u5efa\u5e76\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\u4fe1\u606f\uff1a<\/p>\n<pre>&lt;?php return [\n \/\/ \u9a8c\u8bc1\u7801\u4f4d\u6570\n 'length'    =&gt; 4,\n \/\/ \u9a8c\u8bc1\u7801\u5b57\u7b26\u96c6\u5408\n 'codeSet'   =&amp;gt; '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',\n \/\/ \u9a8c\u8bc1\u7801\u8fc7\u671f\u65f6\u95f4\uff08s\uff09\n 'expire'    =&amp;gt; 1800,\n \/\/ \u9a8c\u8bc1\u7801\u5b57\u4f53\u5927\u5c0f(px)\n 'fontSize'  =&amp;gt; 25,\n \/\/ \u662f\u5426\u753b\u6df7\u6dc6\u66f2\u7ebf\n 'useCurve'  =&amp;gt; true,\n \/\/ \u662f\u5426\u6dfb\u52a0\u6742\u70b9\n 'useNoise'  =&amp;gt; true,\n \/\/ \u9a8c\u8bc1\u7801\u56fe\u7247\u9ad8\u5ea6\n 'imageH'    =&amp;gt; 60,\n \/\/ \u9a8c\u8bc1\u7801\u56fe\u7247\u5bbd\u5ea6\n 'imageW'    =&amp;gt; 220,\n \/\/ \u9a8c\u8bc1\u7801\u80cc\u666f\u989c\u8272\uff08rgb\u6570\u7ec4\uff0c\u8bbe\u7f6e\u4e3anull\u968f\u673a\u989c\u8272\uff09\n 'bg'        =&amp;gt; [243, 251, 254],\n \/\/ \u9a8c\u8bc1\u7801\u5b57\u4f53\u989c\u8272\uff08rgb\u6570\u7ec4\uff0c\u8bbe\u7f6e\u4e3anull\u968f\u673a\u989c\u8272\uff09\n 'fontColor' =&amp;gt; null,\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u4e3a\u4e00\u4e9b\u5e38\u7528\u7684\u9a8c\u8bc1\u7801\u914d\u7f6e\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u3002\u6bcf\u4e2a\u914d\u7f6e\u9879\u7684\u4f5c\u7528\u90fd\u6709\u76f8\u5e94\u7684\u6ce8\u91ca\u8bf4\u660e\u3002<\/p>\n<ol>\n<li>\n<p>\u751f\u6210\u9a8c\u8bc1\u7801<br \/>\u5728\u751f\u6210\u9a8c\u8bc1\u7801\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b9e\u4f8b\u5316\u9a8c\u8bc1\u7801\u5de5\u5177\u7c7b\u3002 \u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5373\u53ef\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>use thinkcaptchaacadeCaptcha;\n\nclass Demo extends Controller\n{\n \/\/ \u751f\u6210\u9a8c\u8bc1\u7801\n public function captcha()\n {\n     return Captcha::create();\n }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u5176\u4e2dcreate()\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u9a8c\u8bc1\u7801\u5b57\u7b26\u4e2a\u6570\uff1b\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9009\u9879\u7684\u6570\u7ec4\uff0c\u5177\u4f53\u8bf4\u660e\u89c1\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>$options = [\n    'length'    =&amp;gt; 4,\n    'fontSize'  =&amp;gt; 25,\n    'imageW'    =&amp;gt; 220,\n    'imageH'    =&amp;gt; 60,\n    'useCurve'  =&amp;gt; false,\n    'useNoise'  =&amp;gt; true,\n    'reset'     =&amp;gt; true,\n    'fontttf'   =&amp;gt; '',\n    'bg'        =&amp;gt; [243, 251, 254],\n    'expire'    =&amp;gt; 1800,\n    'codeSet'   =&amp;gt; '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',\n    'zh'        =&amp;gt; false,\n    'math'      =&amp;gt; false,\n    'addChars'  =&amp;gt; '',\n    'fontColor' =&amp;gt; null,\n];\nreturn Captcha::create($options);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\n<p>\u9a8c\u8bc1\u9a8c\u8bc1\u7801<br \/>\u5728\u7528\u6237\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u9700\u8981\u5bf9\u9a8c\u8bc1\u7801\u8fdb\u884c\u9a8c\u8bc1\u3002\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre>use thinkcaptchaacadeCaptcha;\n\nclass Demo extends Controller\n{\n \/\/ \u9a8c\u8bc1\u9a8c\u8bc1\u7801\n public function checkCaptcha($captcha)\n {\n     if (Captcha::check($captcha)) {\n         \/\/ \u9a8c\u8bc1\u7801\u6b63\u786e\n         return true;\n     } else {\n         \/\/ \u9a8c\u8bc1\u7801\u9519\u8bef\n         return false;\n     }\n }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u5176\u4e2d\u53c2\u6570$captcha\u4e3a\u7528\u6237\u8f93\u5165\u7684\u9a8c\u8bc1\u7801\u5b57\u7b26\u4e32\uff0cCaptcha::check($captcha)\u51fd\u6570\u4f1a\u81ea\u52a8\u5c06\u5176\u4e0esession\u4e2d\u4fdd\u5b58\u7684\u9a8c\u8bc1\u7801\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5982\u679c\u76f8\u7b49\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u9a8c\u8bc1\u7801\u5728ThinkPHP6\u4e2d\u7684\u4f7f\u7528\u3002\u901a\u8fc7\u5b89\u88c5\u6269\u5c55\u3001\u914d\u7f6e\u9a8c\u8bc1\u7801\u3001\u751f\u6210\u9a8c\u8bc1\u7801\u548c\u9a8c\u8bc1\u9a8c\u8bc1\u7801\u56db\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u9a8c\u8bc1\u7801\u529f\u80fd\uff0c\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u9a8c\u8bc1\u7801\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u7f51\u7edc\u5b89\u5168\u7684\u65e5\u76ca\u91cd\u89c6\uff0c\u9a8c\u8bc1\u7801\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u9a8c\u8bc1\u65b9\u5f0f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u9a8c\u8bc1\u7801\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62\u6076\u610f\u653b\u51fb\u548c\u673a\u5668\u4eba\u81ea\u52a8\u5316\u64cd\u4f5c\uff0c\u4fdd\u969c\u4fe1\u606f\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u30026\u4f5c\u4e3a\u4e00\u6b3e\u4f18\u79c0\u7684php\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86\u9a8c\u8bc1\u7801\u529f\u80fd\u7684\u652f\u6301\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528\u9a8c\u8bc1\u7801\u3002 \u5b89\u88c5\u9a8c\u8bc1\u7801\u6269\u5c55ThinkPHP6\u9ed8\u8ba4\u5e76\u4e0d\u5305\u542b\u9a8c\u8bc1\u7801\u6269\u5c55\uff0c\u9700\u8981\u624b\u52a8\u8fdb\u884c\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7composer\u8fdb\u884c\u5b89\u88c5\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a composer require topthink\/think-captcha \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e\u9a8c\u8bc1\u7801\u5728ThinkPHP6\u4e2d\uff0c\u9a8c\u8bc1\u7801\u7684\u914d\u7f6e\u9700\u8981\u5728config\u76ee\u5f55\u4e0b\u7684captcha.php\u6587\u4ef6\u4e2d\u8fdb\u884c\u3002\u8be5\u6587\u4ef6\u9ed8\u8ba4\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u624b\u52a8\u521b\u5efa\u5e76\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\u4fe1\u606f\uff1a &lt;?php return [ \/\/ \u9a8c\u8bc1\u7801\u4f4d\u6570 &#8216;length&#8217; =&gt; 4, \/\/ \u9a8c\u8bc1\u7801\u5b57\u7b26\u96c6\u5408 &#8216;codeSet&#8217; =&amp;gt; &#8216;2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY&#8217;, \/\/ \u9a8c\u8bc1\u7801\u8fc7\u671f\u65f6\u95f4\uff08s\uff09 &#8216;expire&#8217; =&amp;gt; 1800, \/\/ \u9a8c\u8bc1\u7801\u5b57\u4f53\u5927\u5c0f(px) &#8216;fontSize&#8217; =&amp;gt; 25, \/\/ \u662f\u5426\u753b\u6df7\u6dc6\u66f2\u7ebf &#8216;useCurve&#8217; =&amp;gt; true, \/\/ \u662f\u5426\u6dfb\u52a0\u6742\u70b9 &#8216;useNoise&#8217; =&amp;gt; true, \/\/ \u9a8c\u8bc1\u7801\u56fe\u7247\u9ad8\u5ea6 &#8216;imageH&#8217; =&amp;gt; 60, \/\/ \u9a8c\u8bc1\u7801\u56fe\u7247\u5bbd\u5ea6 &#8216;imageW&#8217; =&amp;gt; 220, \/\/ \u9a8c\u8bc1\u7801\u80cc\u666f\u989c\u8272\uff08rgb\u6570\u7ec4\uff0c\u8bbe\u7f6e\u4e3anull\u968f\u673a\u989c\u8272\uff09 &#8216;bg&#8217; =&amp;gt; [243, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23869","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23869"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23869\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}