2014-03-13 54 views
0

我有一個的index.php這條線:PHP如何獲得,其中包括頁面的URL使用

<?php echo (file_get_contents("http://www.mydomain.com/includes/header.php")); ?>

在header.php中,我想包括DIV secion只有訪問者在www.mydomain.com/index.php其餘的我想隱藏這個。

我打算使用$_SERVER['SERVER_NAME']REQUEST_URI來獲取位置,然後使用IF來檢查頁面是否爲index.php ELSE不顯示DIV部分。

的問題是,如果我把它搬進header.php中我得到的網址是www.mydomain.com/includes/header.php insted的www.mydomain.com/index.php

任何的想法如何解決這個問題?

+1

爲什麼一個普通的的file_get_contents instea包括?? – Steve

回答

1

爲什麼要使用file_get_contents是否有特定的原因?您可以使用include_once('header.php');。在你的index.php文件中。確定你的index.php頁面上,你可以在你的header.php文件使用

if(basename($_SERVER['PHP_SELF']) == "index.php") { 
    /* show whatever you need */ 
} 
0

你可以使用定義基於$ _ SERVER [「SCRIPT_NAME」]變量的變量。因此,在index.php文件中,您可以先定義它,然後包含頭文件。

$thisPage = $_SERVER['SCRIPT_NAME']; 
include('includes/header.php'); 

然後,在header.php文件,if語句:

if ($thisPage == 'index.php'){ 
    //all the code 
    //echo this, that, and the other 
} 
相關問題