{"id":23187,"date":"2024-11-21T14:32:04","date_gmt":"2024-11-21T06:32:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23187\/"},"modified":"2024-11-21T14:32:04","modified_gmt":"2024-11-21T06:32:04","slug":"thinkphp%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%adtrace%e8%b0%83%e8%af%95%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23187\/","title":{"rendered":"thinkphp\u5982\u4f55\u5173\u95edtrace\u8c03\u8bd5\u6a21\u5f0f"},"content":{"rendered":"<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u5173\u95ed trace \u6a21\u5f0f\uff1f<\/h3>\n<p>Trace \u6a21\u5f0f\u662f ThinkPHP \u81ea\u5e26\u7684\u8c03\u8bd5\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u9875\u9762\u5e95\u90e8\u65b9\u4fbf\u5730\u67e5\u770b\u5f53\u524d\u8bf7\u6c42\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5982\u8bf7\u6c42\u53c2\u6570\u3001SQL \u8bed\u53e5\u7b49\uff0c\u5bf9\u95ee\u9898\u5b9a\u4f4d\u975e\u5e38\u6709\u5e2e\u52a9\u3002However, in a production environment, we do not want these sensitive information to be leaked and thereby affect the system&#8217;s security.\u3002\u6b64\u5916\uff0c\u8c03\u8bd5\u6a21\u5f0f\u8fd8\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u8017\uff0c\u56e0\u6b64\u6211\u4eec\u6709\u5fc5\u8981\u5c06\u5176\u5173\u95ed\u3002<\/p>\n<h3>\u5982\u4f55\u5173\u95ed trace \u6a21\u5f0f\uff1f<\/h3>\n<p>ThinkPHP \u9ed8\u8ba4\u662f\u5f00\u542f trace \u6a21\u5f0f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e app_debug \u53c2\u6570\u6765\u5173\u95ed trace \u6a21\u5f0f\u3002<\/p>\n<p>\u5728 config \u76ee\u5f55\u4e0b\u7684 app.php \u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>\/\/&nbsp;\u662f\u5426\u5f00\u542f\u5e94\u7528\u8c03\u8bd5\u6a21\u5f0f\n'app_debug'&nbsp;=&amp;gt;&nbsp;env('app_debug',&nbsp;true),<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5c06 app_debug \u7684\u503c\u8bbe\u7f6e\u4e3a false \u5373\u53ef\u5173\u95ed trace \u6a21\u5f0f\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/&nbsp;\u662f\u5426\u5f00\u542f\u5e94\u7528\u8c03\u8bd5\u6a21\u5f0f\n'app_debug'&nbsp;=&amp;gt;&nbsp;false,<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9664\u4e86\u901a\u8fc7\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u6765\u5173\u95ed trace \u6a21\u5f0f\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u63a7\u5236\u5668\uff08\u901a\u5e38\u662f\u57fa\u7840\u63a7\u5236\u5668\uff09\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>\/**\n&nbsp;*&nbsp;\u6784\u9020\u51fd\u6570\n&nbsp;*\n&nbsp;*&nbsp;\u5173\u95ed\u8c03\u8bd5\u6a21\u5f0f\n&nbsp;*\/\npublic&nbsp;function&nbsp;__construct()\n{\n&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct();\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5f00\u53d1\u73af\u5883\u4e0b\uff0c\u4e0d\u5173\u95ed\u8c03\u8bd5\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(config('app_debug'))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5173\u95ed\u8c03\u8bd5\n&nbsp;&nbsp;&nbsp;&nbsp;config('app_trace',&nbsp;false);\n&nbsp;&nbsp;&nbsp;&nbsp;config('app_debug',&nbsp;false);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5728\u63a7\u5236\u5668\u521d\u59cb\u5316\u65f6\u88ab\u8c03\u7528\uff0c\u5982\u679c app_debug \u914d\u7f6e\u4e3a false\uff0c\u5219\u4f1a\u5173\u95ed trace \u6a21\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5173\u95edtrace\u8c03\u8bd5\u6a21\u5f0f\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>\u4e3a\u4ec0\u4e48\u8981\u5173\u95ed trace \u6a21\u5f0f\uff1f Trace \u6a21\u5f0f\u662f ThinkPHP \u81ea\u5e26\u7684\u8c03\u8bd5\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u9875\u9762\u5e95\u90e8\u65b9\u4fbf\u5730\u67e5\u770b\u5f53\u524d\u8bf7\u6c42\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5982\u8bf7\u6c42\u53c2\u6570\u3001SQL \u8bed\u53e5\u7b49\uff0c\u5bf9\u95ee\u9898\u5b9a\u4f4d\u975e\u5e38\u6709\u5e2e\u52a9\u3002However, in a production environment, we do not want these sensitive information to be leaked and thereby affect the system&#8217;s security.\u3002\u6b64\u5916\uff0c\u8c03\u8bd5\u6a21\u5f0f\u8fd8\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u8017\uff0c\u56e0\u6b64\u6211\u4eec\u6709\u5fc5\u8981\u5c06\u5176\u5173\u95ed\u3002 \u5982\u4f55\u5173\u95ed trace \u6a21\u5f0f\uff1f ThinkPHP \u9ed8\u8ba4\u662f\u5f00\u542f trace \u6a21\u5f0f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e app_debug \u53c2\u6570\u6765\u5173\u95ed trace \u6a21\u5f0f\u3002 \u5728 config \u76ee\u5f55\u4e0b\u7684 app.php \u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u4ee5\u4e0b\u914d\u7f6e\uff1a \/\/&nbsp;\u662f\u5426\u5f00\u542f\u5e94\u7528\u8c03\u8bd5\u6a21\u5f0f &#8216;app_debug&#8217;&nbsp;=&amp;gt;&nbsp;env(&#8216;app_debug&#8217;,&nbsp;true), \u767b\u5f55\u540e\u590d\u5236 \u5c06 app_debug \u7684\u503c\u8bbe\u7f6e\u4e3a false \u5373\u53ef\u5173\u95ed trace \u6a21\u5f0f\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b [&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-23187","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23187","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=23187"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23187\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}