{"id":46887,"date":"2024-12-02T17:13:15","date_gmt":"2024-12-02T09:13:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46887\/"},"modified":"2024-12-02T17:13:15","modified_gmt":"2024-12-02T09:13:15","slug":"%e5%a6%82%e4%bd%95%e4%b8%ba-php-%e5%87%bd%e6%95%b0%e5%88%9b%e5%bb%ba%e5%ae%89%e5%85%a8%e6%89%a9%e5%b1%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46887\/","title":{"rendered":"\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u5bf9\u4e8e PHP \u51fd\u6570\u6269\u5c55\u7684\u5b89\u5168\u521b\u5efa\uff0c\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u7f16\u5199\u6269\u5c55\u4ee3\u7801\uff0c\u5e76\u9075\u5faa PHP \u6269\u5c55\u6700\u4f73\u5b9e\u8df5\uff1b\u7f16\u8bd1\u6269\u5c55\uff0c\u751f\u6210\u914d\u7f6e\u811a\u672c\u548c Makefile\uff1b\u5b89\u88c5\u6269\u5c55\uff0c\u7f16\u8bd1\u5e76\u5b89\u88c5\uff1b\u5b9e\u65bd\u5b89\u5168\u63aa\u65bd\uff0c\u4f8b\u5982\u9650\u5236\u51fd\u6570\u8bbf\u95ee\u3001\u4f7f\u7528\u8fc7\u6ee4\u5668\u3001\u907f\u514d\u9519\u8bef\u5904\u7406\u9519\u8bef\u548c\u9650\u5236\u8f93\u51fa\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729852407671b73f7246f6.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u6269\u5c55\u662f\u589e\u5f3a PHP \u6838\u5fc3\u529f\u80fd\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u4eec\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u578b\u548c\u7c7b\u3002\u4f46\u662f\uff0c\u5728\u521b\u5efa\u6269\u5c55\u65f6\u4fdd\u62a4\u5e94\u7528\u7a0b\u5e8f\u514d\u53d7\u5b89\u5168\u5a01\u80c1\u975e\u5e38\u91cd\u8981\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u5206\u6b65\u6307\u5357\uff0c\u6559\u60a8\u5982\u4f55\u5b89\u5168\u5730\u521b\u5efa PHP \u51fd\u6570\u6269\u5c55\u3002<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/strong><\/p>\n<p>\u4f7f\u7528 C \u8bed\u8a00\u7f16\u5199\u6269\u5c55\u4ee3\u7801\u3002\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u9075\u5faa PHP \u6269\u5c55\u6700\u4f73\u5b9e\u8df5\uff0c\u4f8b\u5982\u4f7f\u7528 <code>zend_function_entry<\/code> \u7ed3\u6784\u6765\u6ce8\u518c\u51fd\u6570\u3002<\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u7f16\u8bd1\u6269\u5c55<\/strong><\/p>\n<p>\u4f7f\u7528 <code>phpize<\/code> \u5de5\u5177\u7f16\u8bd1\u6269\u5c55\u3002\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a <code>configure<\/code> \u811a\u672c\u548c\u4e00\u4e2a <code>Makefile<\/code>\u3002<\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5b89\u88c5\u6269\u5c55<\/strong><\/p>\n<p>\u8fd0\u884c <code>configure<\/code> \u811a\u672c\u6765\u51c6\u5907\u7f16\u8bd1\u73af\u5883\u3002\u7136\u540e\u8fd0\u884c <code>make<\/code> \u6765\u7f16\u8bd1\u6269\u5c55\u3002\u6700\u540e\uff0c\u4f7f\u7528 <code>make install<\/code> \u5b89\u88c5\u6269\u5c55\u3002<\/p>\n<p><strong>\u6b65\u9aa4 4\uff1a\u5b89\u5168\u63aa\u65bd<\/strong><\/p>\n<ul>\n<li><strong>\u9650\u5236\u51fd\u6570\u8bbf\u95ee\uff1a<\/strong> \u6839\u636e\u7528\u6237\u6743\u9650\u68c0\u67e5\u51fd\u6570\u7684\u53ef\u8bbf\u95ee\u6027\uff0c\u4f8b\u5982\u68c0\u67e5\u7528\u6237\u7ec4\u6216\u89d2\u8272\u3002<\/li>\n<li><strong>\u4f7f\u7528\u8fc7\u6ee4\u5668\uff1a<\/strong> \u9a8c\u8bc1\u548c\u8fc7\u6ee4\u8f93\u5165\u53c2\u6570\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li><strong>\u907f\u514d\u9519\u8bef\u5904\u7406\u9519\u8bef\uff1a<\/strong> \u7f16\u5199\u5065\u58ee\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\u4ee5\u6355\u83b7\u9519\u8bef\u5e76\u751f\u6210\u6e05\u6670\u7684\u6d88\u606f\uff0c\u800c\u4e0d\u662f\u900f\u9732\u8c03\u8bd5\u4fe1\u606f\u3002<\/li>\n<li><strong>\u9650\u5236\u8f93\u51fa\uff1a<\/strong> \u6839\u636e\u9700\u8981\u5c0f\u5fc3\u8f93\u51fa\u654f\u611f\u6570\u636e\u3002\u8003\u8651\u4f7f\u7528 output escaping \u6216\u7f16\u7801\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>safe_extension<\/code> \u7684\u6269\u5c55\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a <code>is_admin()<\/code> \u51fd\u6570\u6765\u5b89\u5168\u5730\u68c0\u67e5\u7528\u6237\u662f\u5426\u5177\u6709\u7ba1\u7406\u5458\u6743\u9650\uff1a<\/p>\n<pre>ZEND_FUNCTION(is_admin) {\n    zend_bool is_admin = 0;\n\n    \/\/ \u4ece PHP \u4f20\u9012\u7684\u53c2\u6570\n    zval *username;\n    \/\/ \u68c0\u67e5\u7528\u6237\u7ec4\u6216\u89d2\u8272\u4ee5\u786e\u5b9a\u662f\u7ba1\u7406\u5458\u8fd8\u662f\u666e\u901a\u7528\u6237\n    if (ZEND_NUM_ARGS() == 1 &amp;&amp; zend_parse_parameters(ZEND_NUM_ARGS(), \"s\", &amp;username) == SUCCESS) {\n        \/\/ \u83b7\u53d6\u7528\u6237\u540d\u5e76\u68c0\u67e5\u5176\u51ed\u636e\n        const char *user = Z_STRVAL_P(username);\n\n        \/\/ \u68c0\u67e5\u7528\u6237\u6743\u9650\uff0c\u5982\u6570\u636e\u5e93\u67e5\u8be2\n        if (...) {\n            is_admin = 1;\n        }\n    }\n\n    RETURN_BOOL(is_admin);\n}<\/pre>\n<p>\u5728 PHP \u4e2d\u4f7f\u7528\u6269\u5c55\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u52a0\u8f7d\u6269\u5c55\ndl('safe_extension.so');\n\n\/\/ \u68c0\u67e5\u7528\u6237\u662f\u5426\u4e3a\u7ba1\u7406\u5458\nif (is_admin('username')) {\n    \/\/ \u6388\u4e88\u7ba1\u7406\u5458\u8bbf\u95ee\u6743\u9650\n} else {\n    \/\/ \u62d2\u7edd\u8bbf\u95ee\n}<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300a\u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u5bf9\u4e8e PHP \u51fd\u6570\u6269\u5c55\u7684\u5b89\u5168\u521b\u5efa\uff0c\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u7f16\u5199\u6269\u5c55\u4ee3\u7801\uff0c\u5e76\u9075\u5faa PHP \u6269\u5c55\u6700\u4f73\u5b9e\u8df5\uff1b\u7f16\u8bd1\u6269\u5c55\uff0c\u751f\u6210\u914d\u7f6e\u811a\u672c\u548c Makefile\uff1b\u5b89\u88c5\u6269\u5c55\uff0c\u7f16\u8bd1\u5e76\u5b89\u88c5\uff1b\u5b9e\u65bd\u5b89\u5168\u63aa\u65bd\uff0c\u4f8b\u5982\u9650\u5236\u51fd\u6570\u8bbf\u95ee\u3001\u4f7f\u7528\u8fc7\u6ee4\u5668\u3001\u907f\u514d\u9519\u8bef\u5904\u7406\u9519\u8bef\u548c\u9650\u5236\u8f93\u51fa\u3002 \u5982\u4f55\u4e3a PHP \u51fd\u6570\u521b\u5efa\u5b89\u5168\u6269\u5c55 \u7b80\u4ecb PHP \u6269\u5c55\u662f\u589e\u5f3a PHP \u6838\u5fc3\u529f\u80fd\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u4eec\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u578b\u548c\u7c7b\u3002\u4f46\u662f\uff0c\u5728\u521b\u5efa\u6269\u5c55\u65f6\u4fdd\u62a4\u5e94\u7528\u7a0b\u5e8f\u514d\u53d7\u5b89\u5168\u5a01\u80c1\u975e\u5e38\u91cd\u8981\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u5206\u6b65\u6307\u5357\uff0c\u6559\u60a8\u5982\u4f55\u5b89\u5168\u5730\u521b\u5efa PHP \u51fd\u6570\u6269\u5c55\u3002 \u6b65\u9aa4 1\uff1a\u7f16\u5199\u6269\u5c55\u4ee3\u7801 \u4f7f\u7528 C \u8bed\u8a00\u7f16\u5199\u6269\u5c55\u4ee3\u7801\u3002\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u9075\u5faa PHP \u6269\u5c55\u6700\u4f73\u5b9e\u8df5\uff0c\u4f8b\u5982\u4f7f\u7528 zend_function_entry \u7ed3\u6784\u6765\u6ce8\u518c\u51fd\u6570\u3002 \u6b65\u9aa4 2\uff1a\u7f16\u8bd1\u6269\u5c55 \u4f7f\u7528 phpize \u5de5\u5177\u7f16\u8bd1\u6269\u5c55\u3002\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a configure \u811a\u672c\u548c\u4e00\u4e2a Makefile\u3002 \u6b65\u9aa4 3\uff1a\u5b89\u88c5\u6269\u5c55 \u8fd0\u884c configure \u811a\u672c\u6765\u51c6\u5907\u7f16\u8bd1\u73af\u5883\u3002\u7136\u540e\u8fd0\u884c make \u6765\u7f16\u8bd1\u6269\u5c55\u3002\u6700\u540e\uff0c\u4f7f\u7528 make install \u5b89\u88c5\u6269\u5c55\u3002 \u6b65\u9aa4 4\uff1a\u5b89\u5168\u63aa\u65bd \u9650\u5236\u51fd\u6570\u8bbf\u95ee\uff1a \u6839\u636e\u7528\u6237\u6743\u9650\u68c0\u67e5\u51fd\u6570\u7684\u53ef\u8bbf\u95ee\u6027\uff0c\u4f8b\u5982\u68c0\u67e5\u7528\u6237\u7ec4\u6216\u89d2\u8272\u3002 \u4f7f\u7528\u8fc7\u6ee4\u5668\uff1a \u9a8c\u8bc1\u548c\u8fc7\u6ee4\u8f93\u5165\u53c2\u6570\u4ee5\u9632\u6b62\u6ce8\u5165\u653b\u51fb\u3002 [&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-46887","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46887","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=46887"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46887\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}