2013-04-29 173 views
-4

我正在尋找一個PHP函數,它接受一個相對URL並返回它是否是這個URL。如何檢查相對URL是否是PHP中的當前URL?

<?PHP 
function isCurrentPage($page) 
{ 
    //Magic 
} 
?> 

這將被傳遞的值,如"/""/foo/bar"page.php,甚至"foo/page.php?parameter=value"

我的第一次嘗試涉及使用$page == $_SERVER["REQUEST_URI"],但這說"/foo/bar" != "/foo/bar/"。這並不是什麼大問題,但是它帶來的困難是"/foo/bar" != "/foo/bar/index.php?parameter=value"。爲了我的目的,我需要說這些是相同的。

如何判斷當前URL是否傳遞給此函數,並且給定的限制?我寧願選擇一個簡單,強大的解決方案,保證在未來5年內工作,因爲這是一個長期的高利用項目。舊的,不推薦使用的函數和正則表達式是可取的。

  • isCurrentPage("http://example.com/foo/bar")
  • isCurrentPage("http://example.com/foo/bar/")
  • isCurrentPage("http://example.com/foo/bar/index.php")
  • isCurrentPage("http://example.com/foo/bar/index.phps")
  • isCurrentPage("http://example.com/foo/bar/index.phtml")
  • 爲題要,該方法必須在URL http://example.com/foo/bar時返回

  • isCurrentPage("/foo/bar/")
  • isCurrentPage("/foo/bar/index.php")
  • isCurrentPage("/foo/bar?parameter=value")
  • isCurrentPage("/foo/bar/?parameter=value")
  • isCurrentPage("/foo/bar/index.php?parameter=value")
  • isCurrentPage("/foo/bar/index.php#id")
  • isCurrentPage("#id")
  • isCurrentPage("index.php")
  • isCurrentPage("index.php?parameter=value")

等等。

+1

這問題沒有顯示任何研究工作。 **做你的作業很重要**。告訴我們你發現了什麼,***爲什麼它不符合你的需求。這表明你已經花時間去嘗試幫助你自己了,它使我們避免重申明顯的答案,最重要的是它可以幫助你得到更具體和相關的答案。 [FAQ](http://stackoverflow.com/questions/how-to-ask)。 – 2013-04-29 14:31:27

+1

什麼是別名,例如apache'別名/ baz/foo/bar'? – 2013-04-29 14:32:15

+0

@MarcB這不是目前的問題,但我很樂意看到一個解決方案,如果你有一個! – Supuhstar 2013-04-29 14:43:52

回答

6

您可能可以使用parse_url() function來拆分您的網址並清除所有非重要數據,例如查詢字符串。

下面是一個簡單的例子:

$url = 'http://yoursite.com/foo/bar?some=param'; 
$urlParts = parse_url($url); 
// Array 
// (
//  [scheme] => http 
//  [host] => yoursite.com 
//  [path] => /foo/bar 
//  [query] => ?some=param 
//) 

現在,您就可以比較$urlParts['path']對你知路徑列表...

+0

有點不完整,因爲我必須添加好幾行代碼才能使它適用於上面列出的情況,但這是解決方案的手段。謝謝! – Supuhstar 2013-04-29 14:57:26

+1

噢......是的......我的大部分答案都不提供複製粘貼解決方案,而是在正確的方向上微調和一些有用的文檔的有用鏈接... – Lix 2013-04-29 14:59:57

+0

我很高興我可以幫助!快樂的編碼! – Lix 2013-04-29 15:00:45

1

如何:

function isCurrentPage($page) 
{ 
     //Magic 
     $page = preg_replace('/https?:\/\/[a-zA-Z0-9_\.\-]+/', '', $page); 

     $url = 'http'; 
     if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { 
      $url .= 's'; 
     } 
     $url .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $page; 
     $handle = curl_init($url); 
     curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); 

     /* Get the HTML or whatever is linked in $url. */ 
     $response = curl_exec($handle); 

     /* Check for 404 (file not found). */ 
     $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); 
     curl_close($handle); 

     return $httpCode != 404; 
} 
+0

當'$ page'爲'「https://example.com/foo」'時,這會中斷,因爲第一行使'$ url''「http://example.com:80https://example.com/富「'。雖然錯誤檢查爲+1! – Supuhstar 2013-04-29 15:02:50

+0

更新了https支持的代碼謝謝。 – 2013-04-29 16:49:14

+0

問題仍然存在,除了現在它使'$ url''「https://example.com:80https://example.com/foo」' – Supuhstar 2013-04-29 16:50:15