{"id":22936,"date":"2024-11-21T11:38:16","date_gmt":"2024-11-21T03:38:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22936\/"},"modified":"2024-11-21T11:38:16","modified_gmt":"2024-11-21T03:38:16","slug":"thinkphp%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e9%a1%b5%e9%9d%a2%e7%9a%84%e5%88%b7%e6%96%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22936\/","title":{"rendered":"thinkphp\u600e\u4e48\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u9875\u9762\u5237\u65b0\u7684\u57fa\u672c\u542b\u4e49<\/strong><\/p>\n<p>\u5728\u7f51\u7ad9\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9875\u9762\u5237\u65b0\u901a\u5e38\u662f\u6307\u57fa\u4e8e\u5f53\u524d\u9875\u9762\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u3002\u5237\u65b0\u540e\uff0c\u91cd\u65b0\u83b7\u53d6\u6570\u636e\uff0c\u66f4\u65b0\u9875\u9762\u5c55\u793a\u3002\u9875\u9762\u5237\u65b0\u4e00\u822c\u5206\u4e3a\u4e24\u7c7b\uff1a\u524d\u7aef\u5237\u65b0\u548c\u540e\u7aef\u5237\u65b0\u3002<\/p>\n<p>\u5237\u65b0\u524d\u7aef\u9875\u9762\u53ef\u4ee5\u901a\u8fc7\u524d\u7aef\u6280\u672f\uff0c\u6bd4\u5982JavaScript\u3001HTML\u548cCSS\uff0c\u5b9e\u73b0\u9875\u9762\u624b\u52a8\u6216\u81ea\u52a8\u5237\u65b0\u3002\u5b9e\u73b0\u540e\u7aef\u5237\u65b0\u901a\u5e38\u9700\u8981\u7f16\u5199\u540e\u7aef\u811a\u672c\u6216\u8fd0\u7528ajax\u7b49\u6280\u672f\uff0c\u5728\u670d\u52a1\u5668\u7aef\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4e8c\u3001\u4e2d\u524d\u7aef\u5237\u65b0\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728thinkphp\u4e2d\u5b9e\u73b0\u524d\u7aef\u5237\u65b0\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7528jQuery\u7684load()\u65b9\u6cd5\u5c31\u80fd\u5b9e\u73b0\u5c40\u90e8\u5237\u65b0\u3002<\/p>\n<pre>&lt;script&gt;\n$(document).ready(function() {\n    $(\"#refreshBtn\").click(function(){\n        $(\"#data\").load(location.href+\" #data&gt;*\",\"\");\n    });\n});\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u5237\u65b0\u533a\u57df\u7684id\u4e3adata\uff0c\u5237\u65b0\u6309\u94ae\u7684id\u4e3arefreshBtn\u3002\u70b9\u51fb\u5237\u65b0\u6309\u94ae\u540e\uff0c\u901a\u8fc7jQuery\u7684load()\u65b9\u6cd5\uff0c\u91cd\u65b0\u83b7\u53d6\u5f53\u524durl\u4e2ddata\u533a\u57df\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u5c06\u5176\u663e\u793a\u5728\u9875\u9762\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002<\/p>\n<p><strong>\u4e09\u3001thinkphp\u4e2d\u540e\u7aef\u5237\u65b0\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728thinkphp\u4e2d\u5b9e\u73b0\u540e\u7aef\u5237\u65b0\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p>1.\u4f7f\u7528\u539f\u751fPHP\u7684header()\u51fd\u6570<\/p>\n<p>\u901a\u8fc7header()\u51fd\u6570\u5c06\u9875\u9762\u91cd\u5b9a\u5411\u5230\u5f53\u524d\u9875\u9762\uff0c\u4ece\u800c\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002<\/p>\n<pre>header(\"Location:\".$_SERVER['REQUEST_URI']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86$_SERVER[&#8216;REQUEST_URI&#8217;]\u83b7\u53d6\u4e86\u5f53\u524d\u8bf7\u6c42\u7684url\u8def\u5f84\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u9875\u9762\u7684\u91cd\u5b9a\u5411\uff0c\u5b9e\u73b0\u4e86\u9875\u9762\u7684\u5237\u65b0\u3002<\/p>\n<p>2.\u4f7f\u7528thinkphp\u5c01\u88c5\u7684redirect()\u51fd\u6570<\/p>\n<p>redirect()\u51fd\u6570\u662fthinkphp\u5c01\u88c5\u7684\u9875\u9762\u8df3\u8f6c\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9875\u9762\u7684\u91cd\u5b9a\u5411\u548c\u9875\u9762\u53c2\u6570\u4f20\u9012\u7b49\u529f\u80fd\u3002<\/p>\n<pre>return&nbsp;$this-&amp;gt;redirect($_SERVER['REQUEST_URI']);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u6b64\u5904\u4f7f\u7528\u4e86thinkphp\u5c01\u88c5\u7684redirect()\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f20\u9012params\u53c2\u6570\uff0c\u4ee5\u5904\u7406\u7279\u5b9a\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<p><strong>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<ol>\n<li>\n<p>\u5237\u65b0\u540e\u51fa\u73b0\u91cd\u590d\u63d0\u4ea4\u95ee\u9898<\/p>\n<\/li>\n<\/ol>\n<p>\u51fa\u73b0\u8fd9\u79cd\u95ee\u9898\u7684\u4e3b\u8981\u539f\u56e0\u662f\u7531\u4e8e\u7528\u6237\u5728\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u8868\u5355\u6570\u636e\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\uff0c\u5f53\u7528\u6237\u5237\u65b0\u9875\u9762\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u4f1a\u81ea\u52a8\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u6570\u636e\u3002<\/p>\n<p>\u8868\u5355\u7684Token\u9a8c\u8bc1\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Cookie\u6216Session\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003CSRF\u653b\u51fb\u4e0e\u9632\u5fa1\u3002<\/p>\n<ol>\n<li>\n<p>ajax\u5237\u65b0\u9875\u9762\u65e0\u6548<\/p>\n<\/li>\n<\/ol>\n<p>\u51fa\u73b0\u8fd9\u79cd\u95ee\u9898\u7684\u539f\u56e0\u53ef\u80fd\u662fajax\u7684\u7f13\u5b58\u673a\u5236\u5bfc\u81f4\u9875\u9762\u65e0\u6cd5\u5237\u65b0\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u5728ajax\u8bf7\u6c42\u4e2d\u52a0\u5165cache: false\u5c5e\u6027\uff0c\u7981\u6b62ajax\u7f13\u5b58\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\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>\u4e00\u3001\u9875\u9762\u5237\u65b0\u7684\u57fa\u672c\u542b\u4e49 \u5728\u7f51\u7ad9\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9875\u9762\u5237\u65b0\u901a\u5e38\u662f\u6307\u57fa\u4e8e\u5f53\u524d\u9875\u9762\u91cd\u65b0\u52a0\u8f7d\u9875\u9762\u3002\u5237\u65b0\u540e\uff0c\u91cd\u65b0\u83b7\u53d6\u6570\u636e\uff0c\u66f4\u65b0\u9875\u9762\u5c55\u793a\u3002\u9875\u9762\u5237\u65b0\u4e00\u822c\u5206\u4e3a\u4e24\u7c7b\uff1a\u524d\u7aef\u5237\u65b0\u548c\u540e\u7aef\u5237\u65b0\u3002 \u5237\u65b0\u524d\u7aef\u9875\u9762\u53ef\u4ee5\u901a\u8fc7\u524d\u7aef\u6280\u672f\uff0c\u6bd4\u5982JavaScript\u3001HTML\u548cCSS\uff0c\u5b9e\u73b0\u9875\u9762\u624b\u52a8\u6216\u81ea\u52a8\u5237\u65b0\u3002\u5b9e\u73b0\u540e\u7aef\u5237\u65b0\u901a\u5e38\u9700\u8981\u7f16\u5199\u540e\u7aef\u811a\u672c\u6216\u8fd0\u7528ajax\u7b49\u6280\u672f\uff0c\u5728\u670d\u52a1\u5668\u7aef\u8fdb\u884c\u64cd\u4f5c\u3002 \u4e8c\u3001\u4e2d\u524d\u7aef\u5237\u65b0\u5b9e\u73b0\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728thinkphp\u4e2d\u5b9e\u73b0\u524d\u7aef\u5237\u65b0\u5c31\u76f8\u5f53\u7b80\u5355\u4e86\u3002\u7528jQuery\u7684load()\u65b9\u6cd5\u5c31\u80fd\u5b9e\u73b0\u5c40\u90e8\u5237\u65b0\u3002 &lt;script&gt; $(document).ready(function() { $(&#8220;#refreshBtn&#8221;).click(function(){ $(&#8220;#data&#8221;).load(location.href+&#8221; #data&gt;*&#8221;,&#8221;&#8221;); }); }); &lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u9700\u8981\u5237\u65b0\u533a\u57df\u7684id\u4e3adata\uff0c\u5237\u65b0\u6309\u94ae\u7684id\u4e3arefreshBtn\u3002\u70b9\u51fb\u5237\u65b0\u6309\u94ae\u540e\uff0c\u901a\u8fc7jQuery\u7684load()\u65b9\u6cd5\uff0c\u91cd\u65b0\u83b7\u53d6\u5f53\u524durl\u4e2ddata\u533a\u57df\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u5c06\u5176\u663e\u793a\u5728\u9875\u9762\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002 \u4e09\u3001thinkphp\u4e2d\u540e\u7aef\u5237\u65b0\u5b9e\u73b0\u65b9\u6cd5 \u5728thinkphp\u4e2d\u5b9e\u73b0\u540e\u7aef\u5237\u65b0\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a 1.\u4f7f\u7528\u539f\u751fPHP\u7684header()\u51fd\u6570 \u901a\u8fc7header()\u51fd\u6570\u5c06\u9875\u9762\u91cd\u5b9a\u5411\u5230\u5f53\u524d\u9875\u9762\uff0c\u4ece\u800c\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002 header(&#8220;Location:&#8221;.$_SERVER[&#8216;REQUEST_URI&#8217;]); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u4f7f\u7528\u4e86$_SERVER[&#8216;REQUEST_URI&#8217;]\u83b7\u53d6\u4e86\u5f53\u524d\u8bf7\u6c42\u7684url\u8def\u5f84\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u9875\u9762\u7684\u91cd\u5b9a\u5411\uff0c\u5b9e\u73b0\u4e86\u9875\u9762\u7684\u5237\u65b0\u3002 2.\u4f7f\u7528thinkphp\u5c01\u88c5\u7684redirect()\u51fd\u6570 redirect()\u51fd\u6570\u662fthinkphp\u5c01\u88c5\u7684\u9875\u9762\u8df3\u8f6c\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9875\u9762\u7684\u91cd\u5b9a\u5411\u548c\u9875\u9762\u53c2\u6570\u4f20\u9012\u7b49\u529f\u80fd\u3002 return&nbsp;$this-&amp;gt;redirect($_SERVER[&#8216;REQUEST_URI&#8217;]); \u767b\u5f55\u540e\u590d\u5236 \u5728\u6b64\u5904\u4f7f\u7528\u4e86thinkphp\u5c01\u88c5\u7684redirect()\u51fd\u6570\uff0c\u6765\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u3002\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f20\u9012params\u53c2\u6570\uff0c\u4ee5\u5904\u7406\u7279\u5b9a\u7684\u4e1a\u52a1\u903b\u8f91\u3002 \u56db\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6cd5 \u5237\u65b0\u540e\u51fa\u73b0\u91cd\u590d\u63d0\u4ea4\u95ee\u9898 \u51fa\u73b0\u8fd9\u79cd\u95ee\u9898\u7684\u4e3b\u8981\u539f\u56e0\u662f\u7531\u4e8e\u7528\u6237\u5728\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u8868\u5355\u6570\u636e\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\uff0c\u5f53\u7528\u6237\u5237\u65b0\u9875\u9762\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u4f1a\u81ea\u52a8\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u6570\u636e\u3002 \u8868\u5355\u7684Token\u9a8c\u8bc1\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Cookie\u6216Session\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003CSRF\u653b\u51fb\u4e0e\u9632\u5fa1\u3002 ajax\u5237\u65b0\u9875\u9762\u65e0\u6548 \u51fa\u73b0\u8fd9\u79cd\u95ee\u9898\u7684\u539f\u56e0\u53ef\u80fd\u662fajax\u7684\u7f13\u5b58\u673a\u5236\u5bfc\u81f4\u9875\u9762\u65e0\u6cd5\u5237\u65b0\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a\u5728ajax\u8bf7\u6c42\u4e2d\u52a0\u5165cache: false\u5c5e\u6027\uff0c\u7981\u6b62ajax\u7f13\u5b58\u3002 \u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u9875\u9762\u7684\u5237\u65b0\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-22936","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22936","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=22936"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22936\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}