{"id":23730,"date":"2024-11-21T11:15:47","date_gmt":"2024-11-21T03:15:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23730\/"},"modified":"2024-11-21T11:15:47","modified_gmt":"2024-11-21T03:15:47","slug":"thinkphp%e7%99%bb%e5%bd%95%e5%90%8e%e6%80%8e%e4%b9%88%e8%b7%b3%e8%bd%ac-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23730\/","title":{"rendered":"thinkphp\u767b\u5f55\u540e\u600e\u4e48\u8df3\u8f6c"},"content":{"rendered":"<p>\u5728\u7f51\u7ad9\u7684\u5f00\u53d1\u4e2d\uff0c\u767b\u5f55\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u5f53\u7528\u6237\u6210\u529f\u767b\u5f55\u540e\uff0c\u8df3\u8f6c\u5230\u5408\u9002\u7684\u9875\u9762\u4e5f\u662f\u5fc5\u8981\u7684\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u66f4\u597d\u7684\u4f7f\u7528\u7f51\u7ad9\u3002\u5728\u4f7f\u7528 thinkphp \u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u5982\u4f55\u5b9e\u73b0\u767b\u5f55\u540e\u7684\u8df3\u8f6c\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u8be6\u7ec6\u770b\u4e00\u4e0b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u767b\u5f55\u9a8c\u8bc1\u6210\u529f\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7528\u6237\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u5e76\u5c06\u7528\u6237\u72b6\u6001\u5199\u5165 session\u3002<\/p>\n<pre>public function login()\n{\n    \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\n    $username = $this-&gt;request-&gt;param('username');\n    $password = $this-&gt;request-&gt;param('password');\n    \/\/ \u5bf9\u83b7\u53d6\u5230\u7684\u53c2\u6570\u8fdb\u884c\u5224\u65ad\u548c\u5904\u7406\n    if(empty($username) || empty($password)) {\n        $this-&gt;error('\u7528\u6237\u540d\u548c\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a');\n    }\n    \/\/ \u67e5\u8be2\u7528\u6237\u4fe1\u606f\n    $user = Db::name('user')-&gt;where('username',$username)-&gt;find();\n    if(empty($user)) {\n        $this-&gt;error('\u7528\u6237\u540d\u4e0d\u5b58\u5728');\n    }\n    \/\/ \u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u6b63\u786e\n    if(md5($password) !== $user['password']) {\n        $this-&gt;error('\u5bc6\u7801\u9519\u8bef');\n    }\n    \/\/ \u5c06\u7528\u6237\u7684\u4fe1\u606f\u5199\u5165 session\n    session('user_id',$user['id']);\n    session('user_info',$user);\n\n    \/\/ \u767b\u5f55\u6210\u529f\u540e\u8fdb\u884c\u8df3\u8f6c\n    $this-&gt;redirect('\/index');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u7136\u540e\u67e5\u8be2\u7528\u6237\u662f\u5426\u5b58\u5728\uff0c\u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u6b63\u786e\uff0c\u5982\u679c\u90fd\u6b63\u786e\uff0c\u5219\u5c06\u7528\u6237\u4fe1\u606f\u5199\u5165 session\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7$this-&gt;redirect()\u8fdb\u884c\u9875\u9762\u7684\u8df3\u8f6c\u4e86\u3002<\/p>\n<pre>$this-&gt;redirect('\/index');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53c2\u6570\u4e2d\u7684\/index\u8868\u793a\u8df3\u8f6c\u5230\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u7684 index \u65b9\u6cd5\uff0c\u5373\u9ed8\u8ba4\u7684\u9996\u9875\u3002\u5982\u679c\u6211\u4eec\u5e0c\u671b\u8df3\u8f6c\u5230\u5176\u4ed6\u7684\u9875\u9762\uff0c\u4f8b\u5982\u8d2d\u7269\u8f66\u9875\u9762\uff0c\u53ea\u9700\u8981\u4fee\u6539\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<pre>$this-&gt;redirect('\/cart\/index');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u8868\u793a\u8df3\u8f6c\u5230\u6839\u76ee\u5f55\u4e0b\u7684 Cart \u63a7\u5236\u5668\u4e0b\u7684 index \u65b9\u6cd5\u4e2d\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u53e6\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u67d0\u4e00\u4e2a\u5177\u4f53\u7684 URL\uff0c\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9700\u8981\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u8df3\u8f6c\u3002<\/p>\n<pre>$this-&gt;redirect('\/html\/index.html');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0credirect()\u65b9\u6cd5\u9ed8\u8ba4\u662f\u8fdb\u884c\u4e86 302 \u91cd\u5b9a\u5411\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u6c38\u4e45\u6027\u7684\u8df3\u8f6c\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u6765\u6307\u5b9a\u3002<\/p>\n<pre>$this-&gt;redirect('\/index',301);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4ee5 ThinkPHP \u6846\u67b6\u4e3a\u4f8b\uff0c\u5b9e\u73b0\u767b\u5f55\u540e\u8df3\u8f6c\u7684\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u6765\u8fdb\u884c\u4fee\u6539\u548c\u8c03\u6574\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u53ef\u4ee5\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u767b\u5f55\u540e\u600e\u4e48\u8df3\u8f6c\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>\u5728\u7f51\u7ad9\u7684\u5f00\u53d1\u4e2d\uff0c\u767b\u5f55\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u529f\u80fd\uff0c\u5f53\u7528\u6237\u6210\u529f\u767b\u5f55\u540e\uff0c\u8df3\u8f6c\u5230\u5408\u9002\u7684\u9875\u9762\u4e5f\u662f\u5fc5\u8981\u7684\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u66f4\u597d\u7684\u4f7f\u7528\u7f51\u7ad9\u3002\u5728\u4f7f\u7528 thinkphp \u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u5982\u4f55\u5b9e\u73b0\u767b\u5f55\u540e\u7684\u8df3\u8f6c\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u8be6\u7ec6\u770b\u4e00\u4e0b\u3002 \u9996\u5148\uff0c\u5728\u767b\u5f55\u9a8c\u8bc1\u6210\u529f\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7528\u6237\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u5e76\u5c06\u7528\u6237\u72b6\u6001\u5199\u5165 session\u3002 public function login() { \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801 $username = $this-&gt;request-&gt;param(&#8216;username&#8217;); $password = $this-&gt;request-&gt;param(&#8216;password&#8217;); \/\/ \u5bf9\u83b7\u53d6\u5230\u7684\u53c2\u6570\u8fdb\u884c\u5224\u65ad\u548c\u5904\u7406 if(empty($username) || empty($password)) { $this-&gt;error(&#8216;\u7528\u6237\u540d\u548c\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a&#8217;); } \/\/ \u67e5\u8be2\u7528\u6237\u4fe1\u606f $user = Db::name(&#8216;user&#8217;)-&gt;where(&#8216;username&#8217;,$username)-&gt;find(); if(empty($user)) { $this-&gt;error(&#8216;\u7528\u6237\u540d\u4e0d\u5b58\u5728&#8217;); } \/\/ \u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u6b63\u786e if(md5($password) !== $user[&#8216;password&#8217;]) { $this-&gt;error(&#8216;\u5bc6\u7801\u9519\u8bef&#8217;); } \/\/ \u5c06\u7528\u6237\u7684\u4fe1\u606f\u5199\u5165 session session(&#8216;user_id&#8217;,$user[&#8216;id&#8217;]); session(&#8216;user_info&#8217;,$user); \/\/ \u767b\u5f55\u6210\u529f\u540e\u8fdb\u884c\u8df3\u8f6c $this-&gt;redirect(&#8216;\/index&#8217;); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u7136\u540e\u67e5\u8be2\u7528\u6237\u662f\u5426\u5b58\u5728\uff0c\u9a8c\u8bc1\u5bc6\u7801\u662f\u5426\u6b63\u786e\uff0c\u5982\u679c\u90fd\u6b63\u786e\uff0c\u5219\u5c06\u7528\u6237\u4fe1\u606f\u5199\u5165 session\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7$this-&gt;redirect()\u8fdb\u884c\u9875\u9762\u7684\u8df3\u8f6c\u4e86\u3002 $this-&gt;redirect(&#8216;\/index&#8217;); [&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-23730","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23730","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=23730"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23730\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}