{"id":46724,"date":"2024-12-02T09:05:14","date_gmt":"2024-12-02T01:05:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46724\/"},"modified":"2024-12-02T09:05:14","modified_gmt":"2024-12-02T01:05:14","slug":"%e5%9c%a8%e6%89%a9%e5%b1%95-php-%e5%87%bd%e6%95%b0%e6%97%b6%e6%9c%89%e5%93%aa%e4%ba%9b%e5%ae%89%e5%85%a8%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46724\/","title":{"rendered":"\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p>\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\uff0c\u9700\u6ce8\u610f\u4ee5\u4e0b\u5b89\u5168\u4e8b\u9879\uff1a\u9a8c\u8bc1\u8f93\u5165\u3001\u9632\u6b62\u4ee3\u7801\u6ce8\u5165\u3001\u6743\u9650\u68c0\u67e5\u3001\u8f93\u51fa\u7f16\u7801\u3001\u7f13\u5b58\u4e0e\u8fc7\u671f\u3001\u9519\u8bef\u5904\u7406\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u6269\u5c55 filter_var() \u4ee5\u9a8c\u8bc1\u8f93\u5165\uff0c\u5e76\u5728\u9a8c\u8bc1\u6210\u529f\u540e\u6e05\u7406\u8f93\u5165\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729842281671b4c69f180f.jpg\" class=\"aligncenter\" title=\"\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f\u63d2\u56fe\" alt=\"\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6269\u5c55\u7684\u5b89\u5168\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u4ee5\u4e0b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<p><strong>1. \u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<p>\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u6709\u6548\u5e76\u7ecf\u8fc7\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002\u4f7f\u7528 <code>filter_var()<\/code>\u3001<code>preg_match()<\/code> \u548c\u5176\u4ed6\u9a8c\u8bc1\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>function validate_email($email)\n{\n    return filter_var($email, FILTER_VALIDATE_EMAIL);\n}<\/pre>\n<p><strong>2. \u4ee3\u7801\u6ce8\u5165<\/strong><\/p>\n<p>\u9632\u6b62\u901a\u8fc7\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u6765\u6e90\u6ce8\u5165\u4efb\u610f\u4ee3\u7801\u3002\u4f7f\u7528 <code>escapeshellarg()<\/code> \u6216 <code>escapeshellcmd()<\/code> \u5bf9\u4efb\u4f55\u5916\u90e8\u547d\u4ee4\u8c03\u7528\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>system(escapeshellcmd('ls -la'));<\/pre>\n<p><strong>3. \u6743\u9650\u68c0\u67e5<\/strong><\/p>\n<p>\u786e\u4fdd\u4ec5\u5141\u8bb8\u9002\u5f53\u7684\u7528\u6237\u6267\u884c\u51fd\u6570\u3002\u4f7f\u7528 <code>posix_getpwuid()<\/code> \u6216 <code>posix_getpwnam()<\/code> \u68c0\u67e5\u6743\u9650\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>if (posix_getpwuid(posix_geteuid())['name'] !== 'admin') {\n    throw new Exception('Permission denied');\n}<\/pre>\n<p><strong>4. \u8f93\u51fa\u7f16\u7801<\/strong><\/p>\n<p>\u5bf9\u51fd\u6570\u8f93\u51fa\u8fdb\u884c\u7f16\u7801\uff0c\u4ee5\u9632\u6b62\u8de8\u7ad9\u811a\u672c (XSS) \u653b\u51fb\u3002\u4f7f\u7528 <code>htmlspecialchars()<\/code> \u6216 <code>json_encode()<\/code> \u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>echo htmlspecialchars($output);<\/pre>\n<p><strong>5. \u7f13\u5b58\u548c\u8fc7\u671f<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u7ed3\u679c\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u4fdd\u6301\u76f8\u540c\uff0c\u8bf7\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u540c\u65f6\uff0c\u8003\u8651\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u4ee5\u9632\u6b62\u7f13\u5b58\u8fc7\u65f6\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>use Symfony\\Component\\Cache\\Adapter\\FilesystemAdapter;\n$cache = new FilesystemAdapter();\n$cacheItem = $cache-&gt;getItem('my_results');\nif ($cacheItem-&gt;isHit()) {\n    return $cacheItem-&gt;get();\n} else {\n    \/\/ \u8ba1\u7b97\u7ed3\u679c\u5e76\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\n    $cacheItem-&gt;set($results);\n    $cacheItem-&gt;expiresAfter(600);\n    $cache-&gt;save($cacheItem);\n}<\/pre>\n<p><strong>6. \u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u5e76\u5411\u7528\u6237\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u6d88\u606f\u3002\u907f\u514d\u6cc4\u9732\u654f\u611f\u4fe1\u606f\uff0c\u4f8b\u5982\u5185\u90e8\u9519\u8bef\u6216\u5806\u6808\u8ddf\u8e2a\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>try {\n    ...\n} catch (Exception $e) {\n    echo 'An error occurred. Please try again.';\n    \/\/ Log the error for debugging\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6269\u5c55 PHP <code>filter_var()<\/code> \u51fd\u6570\u4ee5\u9a8c\u8bc1\u548c\u6e05\u7406\u8f93\u5165\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>function my_filter_var($value, $flags)\n{\n    \/\/ \u9a8c\u8bc1\u8f93\u5165\u7684\u6709\u6548\u6027\n    if (!filter_var($value, $flags)) {\n        throw new Exception('Invalid input');\n    }\n    \n    \/\/ \u6e05\u7406\u8f93\u5165\n    return htmlspecialchars(trim($value));\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u5b89\u5168,PHP\u6269\u5c55\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f \u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u300a\u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1f\u300b\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01 \u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\uff0c\u9700\u6ce8\u610f\u4ee5\u4e0b\u5b89\u5168\u4e8b\u9879\uff1a\u9a8c\u8bc1\u8f93\u5165\u3001\u9632\u6b62\u4ee3\u7801\u6ce8\u5165\u3001\u6743\u9650\u68c0\u67e5\u3001\u8f93\u51fa\u7f16\u7801\u3001\u7f13\u5b58\u4e0e\u8fc7\u671f\u3001\u9519\u8bef\u5904\u7406\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u6269\u5c55 filter_var() \u4ee5\u9a8c\u8bc1\u8f93\u5165\uff0c\u5e76\u5728\u9a8c\u8bc1\u6210\u529f\u540e\u6e05\u7406\u8f93\u5165\u3002 PHP \u51fd\u6570\u6269\u5c55\u7684\u5b89\u5168\u6ce8\u610f\u4e8b\u9879 \u5728\u6269\u5c55 PHP \u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u8003\u8651\u4ee5\u4e0b\u5b89\u5168\u6ce8\u610f\u4e8b\u9879\uff1a 1. \u8f93\u5165\u9a8c\u8bc1 \u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u6709\u6548\u5e76\u7ecf\u8fc7\u9a8c\u8bc1\uff0c\u4ee5\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002\u4f7f\u7528 filter_var()\u3001preg_match() \u548c\u5176\u4ed6\u9a8c\u8bc1\u65b9\u6cd5\u3002 \u793a\u4f8b\uff1a function validate_email($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); } 2. \u4ee3\u7801\u6ce8\u5165 \u9632\u6b62\u901a\u8fc7\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u6765\u6e90\u6ce8\u5165\u4efb\u610f\u4ee3\u7801\u3002\u4f7f\u7528 escapeshellarg() \u6216 escapeshellcmd() \u5bf9\u4efb\u4f55\u5916\u90e8\u547d\u4ee4\u8c03\u7528\u8fdb\u884c\u8f6c\u4e49\u3002 \u793a\u4f8b\uff1a system(escapeshellcmd(&#8216;ls -la&#8217;)); 3. \u6743\u9650\u68c0\u67e5 \u786e\u4fdd\u4ec5\u5141\u8bb8\u9002\u5f53\u7684\u7528\u6237\u6267\u884c\u51fd\u6570\u3002\u4f7f\u7528 posix_getpwuid() \u6216 posix_getpwnam() \u68c0\u67e5\u6743\u9650\u3002 \u793a\u4f8b\uff1a if (posix_getpwuid(posix_geteuid())[&#8216;name&#8217;] !== &#8216;admin&#8217;) { throw new Exception(&#8216;Permission [&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-46724","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46724","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=46724"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46724\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}