2010-08-02 20 views

回答

1

如果需要檢查完整路徑,請參閱Gumbo's answer。如果index.php只能通過直接導航到該名稱來訪問(也就是說,您知道index.php正在執行中,用戶必須已經去過index.php,並且您沒有使用類似URL重寫的內容),但它可能更有意義只需檢查:

if($_GET['p'] == 'contact') 

index.php之內。如果達到這個條件,index.php正在執行,並且顯然這是用戶所在的頁面。

+0

謝謝,這對我很有用! – Andre 2010-08-02 17:16:05

4

當前請求的URI路徑上加查詢提供$_SERVER['REQUEST_URI']和處理腳本的文件名來運行的代碼塊$_SERVER['SCRIPT_FILENAME']

+0

所以, 如果($ _ SERVER [ 'REQUEST_URI'] == 「的index.php P =聯繫?」){ 代碼 } 它應該工作? – Andre 2010-08-02 17:02:51

+1

@Andre:URI路徑始終以'/'開頭。所以這個值寧願是'/index.php?p = contact'。但是這必須是實際請求的URI。如果您使用一些重寫技術,則外部/內部URI可能會有所不同。 – Gumbo 2010-08-02 17:06:48

+1

@Andre:哦,如果你只是想測試一些特定的URI查詢參數是否設置,使用'isset($ _ GET ['p'])&& $ _GET ['p'] ==='contact''來測試它。 – Gumbo 2010-08-02 17:07:29

0

這就是我所做的(使用.htaccess進行設置)。因爲我沒有分配的頁面檢查。我用PHP_SELF:

<?php 
if (htmlentities($_SERVER["PHP_SELF"]) === "/.../index.php") { // echo $_SERVER["PHP_SELF"] to see your path .. 
      header("Location: ./"); // I am using .htaccess, so I only want the page name and exclude ".php" in the address-bar (URL) 
      die(); 
     } else if (htmlentities($_SERVER["PHP_SELF"]) === "/.../page2.php") { 
      header("Location: page2"); // I am using .htaccess, so I only want the page name and exclude ".php" in the address-bar (URL) 
      die(); 
     } 
?>