﻿{"id":933,"date":"2014-06-02T17:18:45","date_gmt":"2014-06-02T09:18:45","guid":{"rendered":"http:\/\/notes.zerobox.org\/?p=933"},"modified":"2014-06-02T17:18:45","modified_gmt":"2014-06-02T09:18:45","slug":"%e5%88%9b%e5%bb%ba%e9%ab%98%e5%ae%89%e5%85%a8%e6%80%a7php%e7%bd%91%e7%ab%99%e7%9a%84%e5%87%a0%e4%b8%aa%e5%ae%9e%e7%94%a8%e8%a6%81%e7%82%b9","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/933.html","title":{"rendered":"\u521b\u5efa\u9ad8\u5b89\u5168\u6027PHP\u7f51\u7ad9\u7684\u51e0\u4e2a\u5b9e\u7528\u8981\u70b9"},"content":{"rendered":"<p>\u5927\u5bb6\u90fd\u77e5\u9053PHP\u5df2\u7ecf\u662f\u5f53\u524d\u6700\u6d41\u884c\u7684Web\u5e94\u7528\u7f16\u7a0b\u8bed\u8a00\u4e86\u3002\u4f46\u662f\u4e5f\u4e0e\u5176\u4ed6\u811a\u672c\u8bed\u8a00\u4e00\u6837\uff0cPHP\u4e5f\u6709\u51e0\u4e2a\u5f88\u5371\u9669\u7684\u5b89\u5168\u6f0f\u6d1e\u3002\u6240\u4ee5\u5728\u8fd9\u7bc7\u6559\u5b66\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5927\u81f4\u770b\u770b\u51e0\u4e2a\u5b9e\u7528\u7684\u6280\u5de7\u6765\u8ba9\u4f60\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684PHP\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p><strong>\u6280\u5de71\uff1a\u4f7f\u7528\u5408\u9002\u7684\u9519\u8bef\u62a5\u544a<\/strong><\/p>\n<p>\u4e00\u822c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u591a\u7a0b\u5e8f\u5458\u603b\u662f\u5fd8\u4e86\u5236\u4f5c\u7a0b\u5e8f\u9519\u8bef\u62a5\u544a\uff0c\u8fd9\u662f\u6781\u5927\u7684\u9519\u8bef\uff0c\u56e0\u4e3a\u6070\u5f53\u7684\u9519\u8bef\u62a5\u544a\u4e0d\u4ec5\u4ec5\u662f\u6700\u597d\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u4e5f\u662f\u6781\u4f73\u7684\u5b89\u5168\u6f0f\u6d1e\u68c0\u6d4b\u5de5\u5177\uff0c\u8fd9\u80fd\u8ba9\u4f60\u628a\u5e94\u7528\u771f\u6b63\u4e0a\u7ebf\u524d\u5c3d\u53ef\u80fd\u627e\u51fa\u4f60\u5c06\u4f1a\u9047\u5230\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5f53\u7136\u4e5f\u6709\u5f88\u591a\u65b9\u5f0f\u53bb\u542f\u7528\u9519\u8bef\u62a5\u544a\u3002\u6bd4\u5982\u5728 php.in \u914d\u7f6e\u6587\u4ef6\u4e2d\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u5728\u8fd0\u884c\u65f6\u542f\u7528<\/p>\n<p><strong>\u542f\u52a8\u9519\u8bef\u62a5\u544a<\/strong><\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">error_reporting(E_ALL);<\/li>\n<\/ol>\n<p><strong>\u505c\u7528\u9519\u8bef\u62a5\u544a<\/strong><\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">error_reporting(0);<\/li>\n<\/ol>\n<p><strong>\u6280\u5de72\uff1a\u4e0d\u4f7f\u7528PHP\u7684Weak\u5c5e\u6027\u00a0<\/strong>\u6709\u51e0\u4e2aPHP\u7684\u5c5e\u6027\u662f\u9700\u8981\u88ab\u8bbe\u7f6e\u4e3aOFF\u7684\u3002\u4e00\u822c\u5b83\u4eec\u90fd\u5b58\u5728\u4e8ePHP4\u91cc\u9762\uff0c\u800c\u5728PHP5\u4e2d\u662f\u4e0d\u63a8\u8350\u4f7f\u7528\u7684\u3002\u5c24\u5176\u6700\u540e\u5728PHP6\u91cc\u9762\uff0c\u8fd9\u4e9b\u5c5e\u6027\u90fd\u88ab\u79fb\u9664\u4e86\u3002<\/p>\n<p><strong>\u6ce8\u518c\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<p>\u5f53 register_globals \u88ab\u8bbe\u7f6e\u4e3aON\u65f6\uff0c\u5c31\u76f8\u5f53\u4e8e\u8bbe\u7f6eEnvironment\uff0cGET,POST,COOKIE\u6216\u8005Server\u53d8\u91cf\u90fd\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u6b64\u65f6\u4f60\u6839\u672c\u4e0d\u9700\u8981\u53bb\u5199 $_POST[&#8216;username&#8217;]\u6765\u83b7\u53d6\u8868\u5355\u53d8\u91cf&#8217;username&#8217;\uff0c\u53ea\u9700\u8981&#8217;$username&#8217;\u5c31\u80fd\u83b7\u53d6\u6b64\u53d8\u91cf\u4e86\u3002<\/p>\n<p>\u90a3\u4e48\u4f60\u80af\u5b9a\u5728\u60f3\u65e2\u7136\u8bbe\u7f6e register_globals \u4e3a ON \u6709\u8fd9\u4e48\u65b9\u4fbf\u7684\u597d\u5904\uff0c\u90a3\u4e3a\u4ec0\u4e48\u4e0d\u8981\u4f7f\u7528\u5462\uff1f\u56e0\u4e3a\u5982\u679c\u4f60\u8fd9\u6837\u505a\u5c06\u4f1a\u5e26\u6765\u5f88\u591a\u5b89\u5168\u6027\u7684\u95ee\u9898\uff0c\u800c\u4e14\u4e5f\u53ef\u80fd\u4e0e\u5c40\u90e8\u53d8\u91cf\u540d\u79f0\u76f8\u51b2\u7a81\u3002<\/p>\n<p>\u6bd4\u5982\u5148\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">if(\u00a0!empty(\u00a0$_POST[&#8216;username&#8217;]\u00a0)\u00a0&amp;&amp;\u00a0$_POST[&#8216;username&#8217;]\u00a0==\u00a0\u2018test123\u2032\u00a0&amp;&amp;\u00a0!empty(\u00a0$_POST[&#8216;password&#8217;]\u00a0)\u00a0&amp;&amp;\u00a0$_POST[&#8216;password&#8217;]\u00a0==\u00a0\u201cpass123\u2033\u00a0)<\/li>\n<li style=\"color: #5c5c5c;\">{<\/li>\n<li class=\"alt\" style=\"color: inherit;\">\u00a0\u00a0\u00a0\u00a0$<span class=\"attribute\" style=\"color: red;\">access<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">true<\/span>;<\/li>\n<li style=\"color: #5c5c5c;\">}<\/li>\n<\/ol>\n<p>\u5982\u679c\u8fd0\u884c\u671f\u95f4, register_globals \u88ab\u8bbe\u7f6e\u4e3aON\uff0c\u90a3\u4e48\u7528\u6237\u53ea\u9700\u8981\u4f20\u8f93 access=1 \u5728\u4e00\u53e5\u67e5\u8be2\u5b57\u7b26\u4e32\u4e2d\u5c31\u80fd\u83b7\u53d6\u5230PHP\u811a\u672c\u8fd0\u884c\u7684\u4efb\u4f55\u4e1c\u897f\u4e86\u3002<\/p>\n<p><strong>\u5728.htaccess\u4e2d\u505c\u7528\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">php_flag\u00a0register_globals\u00a00<\/li>\n<\/ol>\n<p>\u5728php.ini\u4e2d\u505c\u7528\u5168\u5c40\u53d8\u91cf<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\"><span class=\"attribute\" style=\"color: red;\">register_globals<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">Off<\/span><\/li>\n<\/ol>\n<p>\u505c\u7528\u7c7b\u4f3c magic_quotes_gpc, magic_quotes_runtime, magic_quotes_sybase \u8fd9\u4e9bMagic Quotes<\/p>\n<p>\u5728.htaccess\u6587\u4ef6\u4e2d\u8bbe\u7f6e<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">php_flag\u00a0magic_quotes_gpc\u00a00<\/li>\n<li style=\"color: #5c5c5c;\">php_flag\u00a0magic_quotes_runtime\u00a00<\/li>\n<\/ol>\n<p>\u5728php.ini\u4e2d\u8bbe\u7f6e<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\"><span class=\"attribute\" style=\"color: red;\">magic_quotes_gpc<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">Off<\/span><\/li>\n<li style=\"color: #5c5c5c;\"><\/li>\n<li class=\"alt\" style=\"color: inherit;\"><span class=\"attribute\" style=\"color: red;\">magic_quotes_runtime<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">Off<\/span><\/li>\n<li style=\"color: #5c5c5c;\"><\/li>\n<li class=\"alt\" style=\"color: inherit;\"><span class=\"attribute\" style=\"color: red;\">magic_quotes_sybase<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">Off<\/span><\/li>\n<li style=\"color: #5c5c5c;\"><\/li>\n<\/ol>\n<p><strong>\u6280\u5de73\uff1a\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u00a0<\/strong>\u4f60\u5f53\u7136\u4e5f\u53ef\u4ee5\u9a8c\u8bc1\u7528\u6237\u7684\u8f93\u5165\uff0c\u9996\u5148\u5fc5\u987b\u77e5\u9053\u4f60\u671f\u671b\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u6837\u5c31\u80fd\u5728\u6d4f\u89c8\u5668\u7aef\u505a\u597d\u9632\u5fa1\u7528\u6237\u6076\u610f\u653b\u51fb\u4f60\u7684\u51c6\u5907\u3002<\/p>\n<p><strong>\u6280\u5de74\uff1a\u907f\u514d\u7528\u6237\u8fdb\u884c\u4ea4\u53c9\u7ad9\u70b9\u811a\u672c\u653b\u51fb\u00a0<\/strong>\u5728Web\u5e94\u7528\u4e2d\uff0c\u90fd\u662f\u7b80\u5355\u5730\u63a5\u53d7\u7528\u6237\u8f93\u5165\u8868\u5355\u7136\u540e\u53cd\u9988\u7ed3\u679c\u3002\u5728\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u5982\u679c\u5141\u8bb8HTML\u683c\u5f0f\u8f93\u5165\u5c06\u662f\u975e\u5e38\u5371\u9669\u7684\u4e8b\u60c5\uff0c\u56e0\u4e3a\u8fd9\u4e5f\u5c31\u5141\u8bb8\u4e86JavaScript\u4ee5\u4e0d\u53ef\u9884\u6599\u7684\u65b9\u5f0f\u4fb5\u5165\u540e\u76f4\u63a5\u6267\u884c\u3002\u54ea\u6015\u53ea\u8981\u6709\u4e00\u4e2a\u8fd9\u6837\u6f0f\u6d1e\uff0ccookie\u6570\u636e\u90fd\u53ef\u80fd\u88ab\u76d7\u53d6\u8fdb\u800c\u5bfc\u81f4\u7528\u6237\u7684\u8d26\u6237\u88ab\u76d7\u53d6\u3002<\/p>\n<p><strong>\u6280\u5de75\uff1a\u9884\u9632SQL\u6ce8\u5165\u653b\u51fb\u00a0<\/strong>PHP\u57fa\u672c\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u5de5\u5177\u6765\u4fdd\u62a4\u4f60\u7684\u6570\u636e\u5e93\uff0c\u6240\u4ee5\u5f53\u4f60\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2amysqli_real_escape_string \u51fd\u6570\u3002<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\" style=\"color: inherit;\">$<span class=\"attribute\" style=\"color: red;\">username<\/span>\u00a0=\u00a0<span class=\"attribute-value\" style=\"color: #0000ff;\">mysqli_real_escape_string<\/span>(\u00a0$GET[&#8216;username&#8217;]\u00a0);<\/li>\n<li style=\"color: #5c5c5c;\"><\/li>\n<li class=\"alt\" style=\"color: inherit;\">mysql_query(\u00a0\u201cSELECT\u00a0*\u00a0FROM\u00a0tbl_employee\u00a0WHERE\u00a0<span class=\"attribute\" style=\"color: red;\">username<\/span>\u00a0=\u00a0\u2019\u201d.$username.\u201c\u2018\u201d);<\/li>\n<li style=\"color: #5c5c5c;\"><\/li>\n<\/ol>\n<p>\u597d\u4e86\uff0c\u5728\u8fd9\u7bc7\u7b80\u77ed\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u9610\u8ff0\u4e86\u51e0\u4e2a\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u5ffd\u89c6\u7684PHP\u5b89\u5168\u6027\u95ee\u9898\u3002\u4f46\u662f\u6700\u7ec8\u662f\u5426\u4f7f\u7528\uff0c\u5982\u4f55\u4f7f\u7528\u8fd8\u662f\u5f00\u53d1\u4eba\u5458\u6765\u51b3\u5b9a\u7684\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u5230\u4f60\u4eec\u3002<\/p>\n<p>\u6587\u7ae0\u51fa\u81ea\uff1a<a style=\"color: #0066cc;\" href=\"http:\/\/www.phpzag.com\/useful-tips-for-creating-secure-php-applications\/\" target=\"_top\">phpzag.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u90fd\u77e5\u9053PHP\u5df2\u7ecf\u662f\u5f53\u524d\u6700\u6d41\u884c\u7684Web\u5e94\u7528\u7f16\u7a0b\u8bed\u8a00\u4e86\u3002\u4f46\u662f\u4e5f\u4e0e\u5176\u4ed6\u811a\u672c\u8bed\u8a00\u4e00\u6837 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/933.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[56],"class_list":["post-933","post","type-post","status-publish","format-standard","hentry","tag-php"],"views":436,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=933"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/933\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=933"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}