2015-05-09 80 views
1

我知道這個問題上有更多的話題,我已經閱讀了他們,但我沒有找到答案。PHP包含/未定義變量

我有一個index.php,包含一堆.php包括:header.php,navigation.php,box.php,footer.php,並且在「pages」div裏面,我有一個需要的不同(基於navbar.php或box.php的鏈接)。

navbar.php例如:

<a href="index.php?v=pag1.php">Page1</a> 
<a href="index.php?v=pag2.php">Page2</a> 

的index.php:

<div id="wrapper"> 
    <div id="header"> 
     <div id="topbox"> 
<?php include ("box.php"); ?>  
     </div></div> 
    <div id="content"> 
    <div id="bara"> 
     <div id="navbar"> 
<?php include ("navbar.php"); ?>      
     </div>     
    </div> 
    <div id="pages">  
<?php include ("$v"); ?>   
    </div> 
    </div> 
    <div id="footer"> 
<?php include("footer.php"); ?> 
    </div> 
</div> 

我有一個$ V = 'pag1.php',$ V = 'pag2.php' 等上每一頁我想包括,但我得到「未定義的變量」錯誤「未定義的變量:v在C:\ wamp \ www \ test \ index.php在39行」

如果我定義變量$ v index.php,它的工作原理,但這意味着我將不得不創建重複o f index.php,並有點擊敗目的。我只希望pag1.php,pag2.php,pag3.php等包含在index.php中,而不是創建大量副本。我可以使用HTML的:)

我知道我可能會問一個愚蠢的問題,但我是一個PHP新手,我不知道在哪裏尋找答案。先謝謝你。

+0

嘗試包括:($ V); –

回答

0

嚴格地說,所有你需要的是改變

<?php include ("$v"); ?> 

<?php include $_GET['v']; ?> 

一對夫婦的事情,這裏要注意:

  • URL參數成爲$_GET陣列中的項目,不是自變量。
  • include不需要括號。包含它們並沒有錯,但沒有必要。 See the documentation
  • 你不應該這樣實際使用這個實現,因爲它是非常不安全的。一個非常簡單的URL操作,和一些意圖不佳的人可能會弄清楚你的文件結構。至少,您應該檢查特定的擴展名,或者在include語句中附加特定的目錄名稱。
  • 你也不應該使用這個實現,因爲它沒有正常處理不存在的文件。如果請求的內容文件不存在,您應該發送一條錯誤消息和一個404頭。發送標題必須在發送任何內容之前完成,因此檢查文件是否存在應該在流程的早期完成。

所以,這全都記在心裏,爲的index.php一個更好的實現是:

<?php 
$v = "content/home.php"; // default page 
if (isset($_GET['v'])) { // if the GET variable is set. If no v parameter is provided, asking for $_GET['v'] will cause an error in strict mode. 
    $v = "content/" . $_GET['v'] . ".php"; // keep some control on what can be opened. 
} 
if (!file_exists($v)) { 
    header("HTTP/1.0 404 Not Found"); // this has to be called before ANY content is sent (include newlines). 
    $v = "content/404.php"; 
} 
?> 

<div id="wrapper"> 
    <div id="header"> 
     <div id="topbox"> 
<?php include "box.php"; ?>  
     </div></div> 
    <div id="content"> 
    <div id="bara"> 
     <div id="navbar"> 
<?php include "navbar.php"; ?>      
     </div>     
    </div> 
    <div id="pages">  
<?php include "$v"; ?>   
    </div> 
    </div> 
    <div id="footer"> 
<?php include"footer.php"; ?> 
    </div> 
</div> 
+0

也謝謝詹姆斯K,我完全不知道安全風險。我有很多東西要讀,我猜想。 – VVG

0

你需要調用它之前定義$v,你可以嘗試:

<?php if(isset($_GET['v'])) { 
$v = $_GET['v']; 
} else { 
$v = "home.php"; 
} 
?> 

<div id="wrapper"> 
<div id="header"> 
    <div id="topbox"> 
<?php include ("box.php"); ?>  
    </div></div> 
<div id="content"> 
<div id="bara"> 
    <div id="navbar"> 
<?php include ("navbar.php"); ?>      
    </div>     
</div> 
<div id="pages">  
<?php include ("$v"); ?>   
</div> 
</div> 
<div id="footer"> 
<?php include("footer.php"); ?> 
</div> 

當然你需要有萬一v沒有在URL中設置因此其他home.php默認頁面

+0

謝謝,寶來。 (老實說,我認爲足夠從navbar.php調用v.就像我說的,我是一個新手 – VVG

0

要傳遞URL中的變量,您需要使用$_GET陣列。它將在你的腳本運行之前被自動填充,所以你只需要讀取它。例如,$_GET['v']將包含您通過URL傳入的值(?v=...)。

請注意,最好首先檢查是否設置了該值,否則如果不是,它會向您發出令人討厭的錯誤。下面的代碼是處理未設置參數的簡單方法:

if (isset($_GET['v'])) { 
    include($_GET['v']); 
} 
else { 
    include('default.php'); 
} 

希望能夠解決它。