{"id":25167,"date":"2024-11-21T10:13:54","date_gmt":"2024-11-21T02:13:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25167\/"},"modified":"2024-11-21T10:13:54","modified_gmt":"2024-11-21T02:13:54","slug":"laravel%e7%99%bb%e5%bd%95%e6%97%b6%e9%97%b4%e5%a4%b1%e6%95%88%e5%bc%95%e5%8f%91%e7%9a%84%e7%94%a8%e6%88%b7%e4%bd%93%e9%aa%8c%e4%bc%98%e5%8c%96%e6%8e%a2%e7%a9%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25167\/","title":{"rendered":"Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/170971236773098.jpg\" class=\"aligncenter\" title=\"Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76\u63d2\u56fe\" alt=\"Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76\u63d2\u56fe\" \/><\/p>\n<p>Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76<\/p>\n<p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u7528\u6237\u767b\u5f55\u8fc7\u671f\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5f53\u7528\u6237\u957f\u65f6\u95f4\u4e0d\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u767b\u51fa\u7528\u6237\uff0c\u8fd9\u4f1a\u7ed9\u7528\u6237\u5e26\u6765\u4e00\u5b9a\u7684\u4e0d\u4fbf\u548c\u70e6\u607c\u3002\u7279\u522b\u662f\u5728\u4f7f\u7528Laravel\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<p>\u4e00\u3001\u8bbe\u7f6e\u767b\u5f55\u5931\u6548\u65f6\u95f4<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Laravel\u4e2d\u8bbe\u7f6e\u7528\u6237\u767b\u5f55\u7684\u5931\u6548\u65f6\u95f4\u3002\u901a\u5e38\uff0c\u6211\u4eec\u5728config\/session.php\u914d\u7f6e\u6587\u4ef6\u4e2d\u627e\u5230lifetime\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u51b3\u5b9a\u4e86\u7528\u6237\u7684\u767b\u5f55\u4f1a\u8bdd\u7684\u8fc7\u671f\u65f6\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8bbe\u7f6e\u4e00\u4e2a\u5408\u9002\u7684\u65f6\u95f4\uff0c\u6bd4\u598230\u5206\u949f\u6216\u80051\u5c0f\u65f6\u3002<\/p>\n<pre>'lifetime' =&amp;gt; 30, \/\/ \u5355\u4f4d\u4e3a\u5206\u949f<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u524d\u7aef\u63d0\u793a\u7528\u6237\u767b\u5f55\u5931\u6548<\/p>\n<p>\u5728\u524d\u7aef\u9875\u9762\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7JavaScript\u6765\u5224\u65ad\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\uff0c\u5f53\u7528\u6237\u957f\u65f6\u95f4\u4e0d\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u7528\u6237\u4e00\u4e2a\u63d0\u793a\uff0c\u544a\u8bc9\u4ed6\u4eec\u767b\u5f55\u5df2\u5931\u6548\uff0c\u5e76\u5f15\u5bfc\u5176\u91cd\u65b0\u767b\u5f55\u3002<\/p>\n<pre>var checkSession = function() {\n    $.ajax({\n        url: '\/check-session',\n        type: 'GET',\n        success: function(response) {\n            if (response.status === 'expired') {\n                alert('\u767b\u5f55\u5df2\u5931\u6548\uff0c\u8bf7\u91cd\u65b0\u767b\u5f55\uff01');\n                window.location.href = '\/logout';\n            }\n        }\n    });\n}\n\nsetInterval(checkSession, 60000); \/\/ \u6bcf\u5206\u949f\u68c0\u67e5\u4e00\u6b21\u7528\u6237\u767b\u5f55\u72b6\u6001<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b9a\u65f6\u5668\u6bcf\u5206\u949f\u5411\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2aajax\u8bf7\u6c42\/check-session\uff0c\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\uff0c\u5982\u679c\u5df2\u5931\u6548\uff0c\u5219\u5f39\u51fa\u63d0\u793a\u6846\u5e76\u8df3\u8f6c\u81f3\u767b\u51fa\u9875\u9762\u3002<\/p>\n<p>\u4e09\u3001\u540e\u7aef\u5904\u7406\u7528\u6237\u767b\u5f55\u5931\u6548<\/p>\n<p>\u5728Laravel\u7684Controller\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u7528\u6237\u767b\u5f55\u5931\u6548\u7684\u60c5\u51b5\uff0c\u5f53\u7528\u6237\u7684\u767b\u5f55\u5931\u6548\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u72b6\u6001\u7801\u544a\u8bc9\u524d\u7aef\u7528\u6237\u767b\u5f55\u5df2\u5931\u6548\u3002<\/p>\n<pre>public function checkSession()\n{\n    if (Auth::check()) {\n        return response()-&amp;gt;json(['status' =&amp;gt; 'valid']);\n    } else {\n        return response()-&amp;gt;json(['status' =&amp;gt; 'expired']);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u68c0\u67e5\u7528\u6237\u662f\u5426\u767b\u5f55\u6765\u8fd4\u56de\u4e0d\u540c\u7684\u72b6\u6001\uff0c\u524d\u7aef\u6839\u636e\u8fd4\u56de\u7684\u72b6\u6001\u6765\u63d0\u793a\u7528\u6237\u767b\u5f55\u72b6\u6001\u7684\u6539\u53d8\u3002<\/p>\n<p>\u56db\u3001\u63d0\u4f9b\u91cd\u65b0\u767b\u5f55\u5165\u53e3<\/p>\n<p>\u5f53\u7528\u6237\u7684\u767b\u5f55\u5931\u6548\u65f6\uff0c\u6211\u4eec\u5e94\u8be5\u4e3a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a\u65b9\u4fbf\u7684\u91cd\u65b0\u767b\u5f55\u5165\u53e3\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u7b80\u5355\u5feb\u901f\u5730\u91cd\u65b0\u767b\u5f55\u800c\u4e0d\u5fc5\u91cd\u65b0\u8fdb\u5165\u7f51\u7ad9\u6216\u8fdb\u884c\u7e41\u7410\u7684\u64cd\u4f5c\u3002<\/p>\n<pre>&lt;a href=\"\/login\" class=\"btn btn-primary\"&gt;\u91cd\u65b0\u767b\u5f55&lt;\/a&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u662f\u5bf9Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u7684\u63a2\u7a76\uff0c\u901a\u8fc7\u8bbe\u7f6e\u767b\u5f55\u5931\u6548\u65f6\u95f4\u3001\u524d\u7aef\u63d0\u793a\u3001\u540e\u7aef\u5904\u7406\u4ee5\u53ca\u63d0\u4f9b\u91cd\u65b0\u767b\u5f55\u5165\u53e3\u7b49\u65b9\u5f0f\uff0c\u80fd\u591f\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u51cf\u5c11\u7528\u6237\u7684\u56f0\u6270\uff0c\u4fdd\u8bc1\u7528\u6237\u5bf9\u7f51\u7ad9\u7684\u826f\u597d\u4f53\u9a8c\u3002\u5f53\u7136\uff0c\u9488\u5bf9\u4e0d\u540c\u7684\u9879\u76ee\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u66f4\u52a0\u7ec6\u81f4\u7684\u4f18\u5316\u548c\u5b9a\u5236\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76\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>Laravel\u767b\u5f55\u65f6\u95f4\u5931\u6548\u5f15\u53d1\u7684\u7528\u6237\u4f53\u9a8c\u4f18\u5316\u63a2\u7a76 \u5728Web\u5f00\u53d1\u4e2d\uff0c\u7528\u6237\u767b\u5f55\u8fc7\u671f\u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5f53\u7528\u6237\u957f\u65f6\u95f4\u4e0d\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u767b\u51fa\u7528\u6237\uff0c\u8fd9\u4f1a\u7ed9\u7528\u6237\u5e26\u6765\u4e00\u5b9a\u7684\u4e0d\u4fbf\u548c\u70e6\u607c\u3002\u7279\u522b\u662f\u5728\u4f7f\u7528Laravel\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002 \u4e00\u3001\u8bbe\u7f6e\u767b\u5f55\u5931\u6548\u65f6\u95f4 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Laravel\u4e2d\u8bbe\u7f6e\u7528\u6237\u767b\u5f55\u7684\u5931\u6548\u65f6\u95f4\u3002\u901a\u5e38\uff0c\u6211\u4eec\u5728config\/session.php\u914d\u7f6e\u6587\u4ef6\u4e2d\u627e\u5230lifetime\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u51b3\u5b9a\u4e86\u7528\u6237\u7684\u767b\u5f55\u4f1a\u8bdd\u7684\u8fc7\u671f\u65f6\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8bbe\u7f6e\u4e00\u4e2a\u5408\u9002\u7684\u65f6\u95f4\uff0c\u6bd4\u598230\u5206\u949f\u6216\u80051\u5c0f\u65f6\u3002 &#8216;lifetime&#8217; =&amp;gt; 30, \/\/ \u5355\u4f4d\u4e3a\u5206\u949f \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u524d\u7aef\u63d0\u793a\u7528\u6237\u767b\u5f55\u5931\u6548 \u5728\u524d\u7aef\u9875\u9762\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7JavaScript\u6765\u5224\u65ad\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\uff0c\u5f53\u7528\u6237\u957f\u65f6\u95f4\u4e0d\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u7528\u6237\u4e00\u4e2a\u63d0\u793a\uff0c\u544a\u8bc9\u4ed6\u4eec\u767b\u5f55\u5df2\u5931\u6548\uff0c\u5e76\u5f15\u5bfc\u5176\u91cd\u65b0\u767b\u5f55\u3002 var checkSession = function() { $.ajax({ url: &#8216;\/check-session&#8217;, type: &#8216;GET&#8217;, success: function(response) { if (response.status === &#8216;expired&#8217;) { alert(&#8216;\u767b\u5f55\u5df2\u5931\u6548\uff0c\u8bf7\u91cd\u65b0\u767b\u5f55\uff01&#8217;); window.location.href = &#8216;\/logout&#8217;; } } }); } setInterval(checkSession, 60000); \/\/ \u6bcf\u5206\u949f\u68c0\u67e5\u4e00\u6b21\u7528\u6237\u767b\u5f55\u72b6\u6001 \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b9a\u65f6\u5668\u6bcf\u5206\u949f\u5411\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2aajax\u8bf7\u6c42\/check-session\uff0c\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u5f53\u524d\u7528\u6237\u7684\u767b\u5f55\u72b6\u6001\uff0c\u5982\u679c\u5df2\u5931\u6548\uff0c\u5219\u5f39\u51fa\u63d0\u793a\u6846\u5e76\u8df3\u8f6c\u81f3\u767b\u51fa\u9875\u9762\u3002 \u4e09\u3001\u540e\u7aef\u5904\u7406\u7528\u6237\u767b\u5f55\u5931\u6548 \u5728Laravel\u7684Controller\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u7528\u6237\u767b\u5f55\u5931\u6548\u7684\u60c5\u51b5\uff0c\u5f53\u7528\u6237\u7684\u767b\u5f55\u5931\u6548\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u72b6\u6001\u7801\u544a\u8bc9\u524d\u7aef\u7528\u6237\u767b\u5f55\u5df2\u5931\u6548\u3002 public function checkSession() { if (Auth::check()) { return response()-&amp;gt;json([&#8216;status&#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-25167","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25167","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=25167"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25167\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}