{"id":23089,"date":"2024-11-21T10:47:59","date_gmt":"2024-11-21T02:47:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23089\/"},"modified":"2024-11-21T10:47:59","modified_gmt":"2024-11-21T02:47:59","slug":"thinkphp%e9%a1%b5%e9%9d%a2%e6%89%93%e5%bc%80%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23089\/","title":{"rendered":"thinkphp\u9875\u9762\u6253\u5f00\u4e71\u7801"},"content":{"rendered":"<p>\u5982\u679c\u4f60\u5728\u4f7f\u7528 thinkphp \u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u65f6\u9047\u5230\u4e86\u9875\u9762\u6253\u5f00\u4e71\u7801\u7684\u95ee\u9898\uff0c\u90a3\u4e48\u53ef\u80fd\u662f\u7531\u4e8e\u591a\u79cd\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u68c0\u67e5\u7f16\u7801\u683c\u5f0f<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u9700\u8981\u68c0\u67e5\u5728\u4f60\u7684 PHP \u6587\u4ef6\u4e2d\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002\u5728 ThinkPHP \u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528 UTF-8 \u7f16\u7801\u683c\u5f0f\uff0c\u5982\u679c\u4f60\u5728\u6587\u4ef6\u5934\u4e2d\u6307\u5b9a\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u4e71\u7801\u7684\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u5934\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff1a<\/p>\n<pre>header(\"Content-type: text\/html; charset=utf-8\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u4f60\u9700\u8981\u5728 header() \u51fd\u6570\u4e2d\u6307\u5b9a\u76f8\u5e94\u7684\u53c2\u6570\u3002<\/p>\n<ol>\n<li>\u68c0\u67e5\u670d\u52a1\u5668\u914d\u7f6e<\/li>\n<\/ol>\n<p>\u53e6\u4e00\u4e2a\u53ef\u80fd\u7684\u95ee\u9898\u662f\u670d\u52a1\u5668\u914d\u7f6e\u4e0d\u6b63\u786e\u3002\u4f60\u9700\u8981\u68c0\u67e5\u670d\u52a1\u5668\u662f\u5426\u6b63\u786e\u5730\u914d\u7f6e\u4e86\u5b57\u7b26\u7f16\u7801\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5728 php.ini \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>default_charset = \"utf-8\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u786e\u4fdd\u4fdd\u5b58\u5e76\u4e14\u91cd\u542f\u670d\u52a1\u5668\u540e\u751f\u6548\u3002\u5982\u679c\u4f60\u65e0\u6cd5\u8bbf\u95ee php.ini \u6587\u4ef6\uff0c\u90a3\u4e48\u4f60\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u5728 .htaccess \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>AddDefaultCharset utf-8<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u68c0\u67e5\u6570\u636e\u5e93\u7f16\u7801<\/li>\n<\/ol>\n<p>\u5982\u679c\u4f60\u7684\u7f51\u7ad9\u4f7f\u7528\u4e86\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u4f60\u4e5f\u9700\u8981\u68c0\u67e5\u6570\u636e\u5e93\u7684\u7f16\u7801\u683c\u5f0f\u3002\u5728 ThinkPHP \u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528 UTF-8 \u7f16\u7801\u683c\u5f0f\uff0c\u5982\u679c\u4f60\u5728\u6570\u636e\u5e93\u4e2d\u4f7f\u7528\u4e86\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u4e71\u7801\u7684\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9a\u6570\u636e\u5e93\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>'params' =&amp;gt; [\n    PDO::MYSQL_ATTR_INIT_COMMAND =&amp;gt; 'SET NAMES utf8',\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u786e\u4fdd\u4fdd\u5b58\u5e76\u4e14\u91cd\u542f\u670d\u52a1\u5668\u540e\u751f\u6548\u3002<\/p>\n<ol>\n<li>\u68c0\u67e5\u6d4f\u89c8\u5668\u8bbe\u7f6e<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u4f60\u9700\u8981\u68c0\u67e5\u4f60\u7684\u6d4f\u89c8\u5668\u8bbe\u7f6e\u662f\u5426\u6b63\u786e\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u9009\u62e9\u201c\u67e5\u770b-\u7f16\u7801\u201d\u83dc\u5355\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982 UTF-8\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728\u4f7f\u7528 ThinkPHP \u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u65f6\uff0c\u5982\u679c\u4f60\u9047\u5230\u4e86\u9875\u9762\u6253\u5f00\u4e71\u7801\u7684\u95ee\u9898\uff0c\u90a3\u4e48\u9700\u8981\u68c0\u67e5\u7f16\u7801\u683c\u5f0f\u3001\u670d\u52a1\u5668\u914d\u7f6e\u3001\u6570\u636e\u5e93\u7f16\u7801\u548c\u6d4f\u89c8\u5668\u8bbe\u7f6e\u7b49\u591a\u4e2a\u65b9\u9762\uff0c\u627e\u5230\u95ee\u9898\u6240\u5728\u5e76\u8fdb\u884c\u89e3\u51b3\u3002\u5982\u679c\u4ee5\u4e0a\u65b9\u6cd5\u5747\u65e0\u6548\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e9b\u8c03\u8bd5\u5de5\u5177\u6765\u8fdb\u4e00\u6b65\u6392\u67e5\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u9875\u9762\u6253\u5f00\u4e71\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>\u5982\u679c\u4f60\u5728\u4f7f\u7528 thinkphp \u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\u65f6\u9047\u5230\u4e86\u9875\u9762\u6253\u5f00\u4e71\u7801\u7684\u95ee\u9898\uff0c\u90a3\u4e48\u53ef\u80fd\u662f\u7531\u4e8e\u591a\u79cd\u539f\u56e0\u5bfc\u81f4\u7684\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6cd5\u3002 \u68c0\u67e5\u7f16\u7801\u683c\u5f0f \u9996\u5148\uff0c\u9700\u8981\u68c0\u67e5\u5728\u4f60\u7684 PHP \u6587\u4ef6\u4e2d\u6307\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002\u5728 ThinkPHP \u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528 UTF-8 \u7f16\u7801\u683c\u5f0f\uff0c\u5982\u679c\u4f60\u5728\u6587\u4ef6\u5934\u4e2d\u6307\u5b9a\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u4e71\u7801\u7684\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u5934\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff1a header(&#8220;Content-type: text\/html; charset=utf-8&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u4f60\u9700\u8981\u5728 header() \u51fd\u6570\u4e2d\u6307\u5b9a\u76f8\u5e94\u7684\u53c2\u6570\u3002 \u68c0\u67e5\u670d\u52a1\u5668\u914d\u7f6e \u53e6\u4e00\u4e2a\u53ef\u80fd\u7684\u95ee\u9898\u662f\u670d\u52a1\u5668\u914d\u7f6e\u4e0d\u6b63\u786e\u3002\u4f60\u9700\u8981\u68c0\u67e5\u670d\u52a1\u5668\u662f\u5426\u6b63\u786e\u5730\u914d\u7f6e\u4e86\u5b57\u7b26\u7f16\u7801\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5728 php.ini \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a default_charset = &#8220;utf-8&#8221; \u767b\u5f55\u540e\u590d\u5236 \u786e\u4fdd\u4fdd\u5b58\u5e76\u4e14\u91cd\u542f\u670d\u52a1\u5668\u540e\u751f\u6548\u3002\u5982\u679c\u4f60\u65e0\u6cd5\u8bbf\u95ee php.ini \u6587\u4ef6\uff0c\u90a3\u4e48\u4f60\u4e5f\u53ef\u4ee5\u5c1d\u8bd5\u5728 .htaccess \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b AddDefaultCharset utf-8 \u767b\u5f55\u540e\u590d\u5236 \u68c0\u67e5\u6570\u636e\u5e93\u7f16\u7801 \u5982\u679c\u4f60\u7684\u7f51\u7ad9\u4f7f\u7528\u4e86\u6570\u636e\u5e93\uff0c\u90a3\u4e48\u4f60\u4e5f\u9700\u8981\u68c0\u67e5\u6570\u636e\u5e93\u7684\u7f16\u7801\u683c\u5f0f\u3002\u5728 ThinkPHP \u4e2d\uff0c\u9ed8\u8ba4\u4f7f\u7528 UTF-8 \u7f16\u7801\u683c\u5f0f\uff0c\u5982\u679c\u4f60\u5728\u6570\u636e\u5e93\u4e2d\u4f7f\u7528\u4e86\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u90a3\u4e48\u4f1a\u5bfc\u81f4\u4e71\u7801\u7684\u95ee\u9898\u3002\u4f60\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9a\u6570\u636e\u5e93\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a &#8216;params&#8217; =&amp;gt; [ PDO::MYSQL_ATTR_INIT_COMMAND =&amp;gt; &#8216;SET NAMES utf8&#8217;, ], \u767b\u5f55\u540e\u590d\u5236 \u786e\u4fdd\u4fdd\u5b58\u5e76\u4e14\u91cd\u542f\u670d\u52a1\u5668\u540e\u751f\u6548\u3002 \u68c0\u67e5\u6d4f\u89c8\u5668\u8bbe\u7f6e \u6700\u540e\uff0c\u4f60\u9700\u8981\u68c0\u67e5\u4f60\u7684\u6d4f\u89c8\u5668\u8bbe\u7f6e\u662f\u5426\u6b63\u786e\u3002\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u9009\u62e9\u201c\u67e5\u770b-\u7f16\u7801\u201d\u83dc\u5355\uff0c\u786e\u4fdd\u9009\u62e9\u4e86\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982 UTF-8\u3002 [&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-23089","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23089","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=23089"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23089\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}