2012-02-04 27 views
2

我需要檢測頁面做一個網站上的一些風格,現在我使用如何使用php檢測當前頁面?

$currentpage = $_SERVER['REQUEST_URI']; 

但這返回類似/development/en/contact.php
我想的東西,將只返回contact.php
有沒有一種方法來實現這一目標?

+0

請參閱php文檔鏈接:http://php.net/manual/en/reserved.variables.server.php – mgraph 2012-02-04 12:18:23

回答

8
basename($_SERVER['SCRIPT_NAME']); 

它將只返回文件並剝離文件夾。欲瞭解更多信息,請參閱basename()

注意SCRIPT_NAME的使用,而不是REQUEST_URIREQUEST_URI可能含有額外的 「廢話」:

http://example.com/dev/en/contact.php/this/is/crap 
REQUEST_URI: /dev/en/contact.php/this/is/crap 
SCRIPT_NAME: /dev/en/contact.php 
1
substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1); 
3

這應該得到你想要的東西:

basename($_SERVER["SCRIPT_FILENAME"]); 

雖然要小心,因爲它會給你在不同位置具有相同名稱的文件的相同答案。

相關問題