php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之間的區別_PHP教程

來源:互聯網
上載者:User
“PHP_SELF”
當前正在執行指令碼的檔案名稱,與 document root 相關。舉例來說,在 URL 地址為 http://www.jb51.net/test.php/foo.bar 的指令碼中使用 $_SERVER['PHP_SELF'] 將會得到 /test.php/foo.bar 這個結果。__FILE__ 常量包含當前(例如包含)檔案的絕對路徑和檔案名稱。

“SCRIPT_NAME”
包含當前指令碼的路徑。這在頁面需要指向自己時非常有用。__FILE__ 包含當前檔案的絕對路徑和檔案名稱(例如包含檔案)。

主要的原因如像:$_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];

找到更好的原因是:
今天在Dreamhost上安裝一套PHP程式時發現連接的地址都會多出一個cgi-system來,但是程式的config並沒有問題,查了查資料才發現是SCRIPT_NAME和PHP_SELF不同造成的問題。
通常在本機測試 $_SERVER['SCRIPT_NAME'] 跟 $_SERVER['PHP_SELF'] 大概看不出有什麼不同,因為大部分的PHP不是以CGI模式運行的。
但 DreamHost 上的 PHP 是以 CGI 方式運行,二者就有明顯不同的差異。
echo $_SERVER['SCRIPT_NAME']; // (/cgi-system/php.cgi)
echo $_SERVER['PHP_SELF']; // (/admin/test.php)

從http://lists.nyphp.org/pipermail/talk/2005-July/015339.html 發現了一個說明。老外說的。

SCRIPT_NAME solves all the problems mentioned
in this thread - it's just the script name, without any extra garbage
that might be tacked on by the user. PHP_SELF explicitly includes that
extra garbage, so solutions in this thread that involve stripping the
garbage off of PHP_SELF to make it safe are really, really missing the
point - just use SCRIPT_NAME instead. Please don't use FORM ACTION=”";
according to the spec, what the browser does with that is undefined, so
even if it works in current browsers, it might not work in future ones

http://www.bkjia.com/PHPjc/320515.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320515.htmlTechArticle“PHP_SELF” 當前正在執行指令碼的檔案名稱,與 document root 相關。舉例來說,在 URL 地址為 http://www.jb51.net/test.php/foo.bar 的指令碼中使用 $_SERVER['P...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.