{"id":168,"date":"2011-05-11T14:11:21","date_gmt":"2011-05-11T14:11:21","guid":{"rendered":"006f52e9102a8d3be2fe5614f42ba989"},"modified":"2011-05-11T14:11:21","modified_gmt":"2011-05-11T14:11:21","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/168","title":{"rendered":"php\u4e2d\u7684\u4e0e\u5f53\u524d\u6587\u4ef6\u540d\u6709\u5173\u4e00\u4e9b\u5e38\u91cf"},"content":{"rendered":"<p>__FILE__ ,<br \/>\ngetcwd(),<br \/>\n$_SERVER[\"REQUEST_URI\"],<br \/>\n$_SERVER[\"SCRIPT_NAME\"],<br \/>\n$_SERVER[\"PHP_SELF\"],<br \/>\n$_SERVER[\"SCRIPT_FILENAME\"],<br \/>\n\u6765\u89c2\u5bdf\u4e00\u4e0b\u8fd9\u4e9b\u53d8\u91cf\u6216\u51fd\u6570\u7684\u5f02\u540c.<br \/>\n\u5047\u8bbe\u6709\u4e00\u4e2a\u8bf7\u6c42\u5730\u5740\u4e3a:<br \/>\n<a href=\"http:\/\/localhost:8080\/test.php\/age=20\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php\/age=20<\/span><\/a><br \/>\n\u800ctest.php \u7684\u5b8c\u6574\u8def\u5f84\u662f: D:\/server\/www\/example\/test.php<br \/>\n1) getcwd()<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684\u9875\u9762\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55. \u5373test.php \u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55: D:\/server\/www\/example\/ ,<br \/>\n\u5982\u679c\u5728test.php \u6267\u884c\u4e86 require \u6216 include \u8bed\u53e5, \u6bd4\u5982 inculde(\u201dtest_dir\/test2.php\u201d),<br \/>\n\u90a3\u4e48\u5728 test2.php \u91cc getcwd()\u51fd\u6570 \u8fd4\u56de\u7684\u4e5f\u5c06\u662f test.php \u6240\u5728\u7684\u76ee\u5f55.<br \/>\n2) __FILE__<br \/>\n\u4e00\u4e2a\u9b54\u672f\u53d8\u91cf, \u7528\u5b83\u5c06\u5f97\u5230 __FILE__ \u53d8\u91cf\u6240\u5728\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84,<br \/>\n\u6bd4\u5982: test.php \u91cc __FILE__ \u5c06\u5f97\u5230 D:\/server\/www\/example\/test.php ,<br \/>\ntest_dir\/test2.php \u91cc\u7684 __FILE__ \u5c06\u5f97\u5230 D:\/server\/www\/example\/test_dir\/test2.php<br \/>\n3) $_SERVER[\"SCRIPT_FILENAME\"]<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684\u9875\u9762\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84.<br \/>\ntest.php \u548c test_dir\/test2.php \u91cc\u7528 $_SERVER[\"SCRIPT_NAME\"] \u90fd\u5c06\u5f97\u5230 D:\/server\/www\/example\/test.php.<br \/>\n4) $_SERVER[\"SCRIPT_NAME\"]<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684\u9875\u9762\u6587\u4ef6\u7684\u6587\u4ef6\u540d,\u6ce8\u610f: \u4e0e $_SERVER[\"SCRIPT_NAME\"] \u4e0d\u540c, \u6b64\u53d8\u91cf\u53ea\u5f97\u5230\u6587\u4ef6\u540d\u800c\u4e0d\u5305\u542b\u8def\u5f84,<br \/>\n\u5728test.php \u4e0e test_dir\/test2.php \u7528$_SERVER[\"SCRIPT_NAME\"] \u5f97\u5230\u7684\u90fd\u5c06\u662f test.php.<br \/>\n\u5f53\u7136, \u5728test.php \u4e0e test_dir\/test2.php \u6267\u884c basename($_SERVER[\"SCRIPT_FILENAME\"]) \u4e0e $_SERVER[\"SCRIPT_NAME\"] \u76f8\u540c.<br \/>\n\u6267\u884c \u5728test.php \u4e0e test_dir\/test2.php \u6267\u884c realpath(\u201dtest.php\u201d) \u5f97\u5230\u7684\u7ed3\u679c\u4e0e $_SERVER[\"SCRIPT_FILENAME\"] \u76f8\u540c.<br \/>\n5) $_SERVER[\"PHP_SELF\"]<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u9875\u9762\u7684\u6587\u4ef6\u540d, \u5e76\u5265\u6389\u95ee\u53f7 ? \u540e\u7684\u5185\u5bb9, \u6ce8\u610f:\u4e0d\u5305\u542b\u8def\u5f84,<br \/>\n\u6bd4\u5982\u5728\u5ba2\u6237\u7aef\u91cc\u8bf7\u6c42<br \/>\n<a href=\"http:\/\/localhost:8080\/test.php?age=20&amp;name=Tom\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php?age=20&amp;name=Tom<\/span><\/a><br \/>\n,<br \/>\n\u90a3\u4e48test.php \u548c test_dir\/test2.php \u7684 $_SERVER[\"PHP_SELF\"] \u90fd\u5c06\u5f97\u5230 \u201ctest.php\u201d\u3002\u201cage=20&amp;name=Tom\u201d\u88ab\u5265\u6389\u3002<br \/>\n\u800c\u5982\u679c\u5ba2\u6237\u7aef\u91cc\u8bf7\u6c42<br \/>\n<a href=\"http:\/\/localhost:8080\/test.php\/age=20&amp;name=Tom\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php\/age=20&amp;name=Tom<\/span><\/a><br \/>\n,<br \/>\n\u90a3\u4e48test.php \u548c test_dir\/test2.php \u7684 $_SERVER[\"PHP_SELF\"] \u90fd\u5c06\u5f97\u5230 \u201ctest.php\/age=20&amp;name=Tom\u201d\u3002<br \/>\n6) $_SERVER[\"REQUEST_URI\"]<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u9875\u9762\u7684\u6587\u4ef6\u540d, \u4ee5\u53ca\u6587\u4ef6\u540d\u4e4b\u540e\u7684\u6240\u6709\u5185\u5bb9(\u6ce8\u610f: \u4e95\u53f7 # \u4e4b\u540e\u7684\u5185\u5bb9\u5c06\u88ab\u7565\u53bb),<br \/>\n\u6bd4\u5982\u5728\u5ba2\u6237\u7aef\u91cc\u8bf7\u6c42<br \/>\n<a href=\"http:\/\/localhost:8080\/test.php?age=20&amp;name=Tom\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php?age=20&amp;name=Tom<\/span><\/a><br \/>\n,<br \/>\n\u90a3\u4e48test.php \u548c test_dir\/test2.php \u7684 $_SERVER[\"REUEST_URI\"] \u90fd\u5c06\u5f97\u5230 \u201ctest.php\u201d\u3002\u201cage=20&amp;name=Tom\u201d\u88ab\u5265\u6389\u3002<br \/>\n\u800c\u5982\u679c\u5ba2\u6237\u7aef\u91cc\u8bf7\u6c42<br \/>\n<a href=\"http:\/\/localhost:8080\/test.php\/age=20&amp;name=Tom\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php\/age=20&amp;name=Tom<\/span><\/a><br \/>\n,<br \/>\n\u90a3\u4e48test.php \u548c test_dir\/test2.php \u7684 $_SERVER[\"REQUEST_URI\"] \u90fd\u5c06\u5f97\u5230 \u201ctest.php\/age=20&amp;name=Tom\u201d\u3002<br \/>\n*\/<br \/>\n\/\/ test.php:<br \/>\necho \u201ctest1.php variables<br \/>\n\u201d;<br \/>\necho \u201cgetcwd: \u201c, getcwd(), \u201c<br \/>\n\u201d;<br \/>\necho \u201c__FILE__: \u201c, __FILE__, \u201c<br \/>\n\u201d;<br \/>\necho \u201cREQUEST_URI: \u201c, $_SERVER[\"REQUEST_URI\"], \u201c<br \/>\n\u201d;<br \/>\necho \u201cSCRIPT_NAME: \u201c, $_SERVER[\"SCRIPT_NAME\"], \u201c<br \/>\n\u201d;<br \/>\necho \u201cPHP_SELF: \u201c, $_SERVER[\"PHP_SELF\"], \u201c<br \/>\n\u201d;<br \/>\necho \u201cSCRIPT_FILENAME \u201c, $_SERVER[\"SCRIPT_FILENAME\"] , \u201c<br \/>\n\u201d;<br \/>\n\/\/ \u628a test2.php \u5305\u542b\u8fdb\u6765, \u5728 test2.php \u91cc\u8f93\u51fa\u4e0a\u9762\u7684\u53d8\u91cf,\u770b\u6709\u4ec0\u4e48\u4e0d\u540c:<br \/>\ninclude_once(\u201dtest2\/test2.php\u201d);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>__FILE__ ,<br \/>\ngetcwd(),<br \/>\n$_SERVER[\"REQUEST_URI\"],<br \/>\n$_SERVER[\"SCRIPT_NAME\"],<br \/>\n$_SERVER[\"PHP_SELF\"],<br \/>\n$_SERVER[\"SCRIPT_FILENAME\"],<br \/>\n\u6765\u89c2\u5bdf\u4e00\u4e0b\u8fd9\u4e9b\u53d8\u91cf\u6216\u51fd\u6570\u7684\u5f02\u540c.<br \/>\n\u5047\u8bbe\u6709\u4e00\u4e2a\u8bf7\u6c42\u5730\u5740\u4e3a:<br \/><a href=\"http:\/\/localhost:8080\/test.php\/age=20\" target=\"_blank\"><span style=\"color:#0000ff;\">http:\/\/localhost:8080\/test.php\/age=20<\/span><\/a><br \/>\n\u800ctest.php \u7684\u5b8c\u6574\u8def\u5f84\u662f: D:\/server\/www\/example\/test.php<br \/>\n1) getcwd()<br \/>\n\u5c06\u5f97\u5230\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684\u9875\u9762\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55. \u5373test.php \u6587\u4ef6\u6240... <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/168\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[27],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-article","tag-php"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}