简体   繁体   中英

PHP include / undefined variable

I am aware there are more topics on this matter, I've read them, but I did not found the answer.

I have an index.php, that contains a bunch of .php includes: header.php, navigation.php, box.php, footer.php, and inside the "pages" div, I have the one that needs to be different (based on the links from navbar.php or box.php).

navbar.php example:

<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>

I have a $v='pag1.php', $v='pag2.php' , etc on each page I want to include, but I get "undefined variable" error "Undefined variable: v in C:\\wamp\\www\\test\\index.php on line 39"

If I define the variable $v inside of index.php, it works, but that means I will have to create duplicates of index.php, and kinda defeats the purpose. I only want pag1.php, pag2.php, pag3.php, etc to be included in index.php, not to create a lot of copies. I can use html for that :)

I know I probably ask a stupid question, but I'm a newbie in php, and I have no idea where to look for answers. Thank you in advance.

You need to define $v before calling it, you could try:

<?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>  

Of course you need to have a default page in case v is not set in the URL hence the else home.php

To pass variables in the URL, you need to use the $_GET array. It will be populated automatically before your script is ran, so you only need to read from it. For example, $_GET['v'] will contain the value you have passed in though the URL ( ?v=... ).

Note that it is always best to first check if the value is set, or else it will throw a nasty error at you in case it isn't. The following code is a simple way to handle an unset parameter:

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

Hope this clears it up.

Strictly speaking, all you need is to change

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

to

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

A couple things to note here:

  • URL parameters become items in the $_GET array, not independent variables.
  • include doesn't need parenthesis. It's not wrong to include them, but it's not necessary. See the documentation .
  • You shouldn't actually use this implementation as it is because it's extremely insecure. A very simply bit of URL manipulation, and some fellow with poor intentions could figure out a lot about your file structure. At the very least, you should check for a particular extension, or append a particular directory name in the include statement.
  • You also shouldn't use this implementation as it is, because it doesn't deal with non-existent files gracefully. If the requested content file doesn't exist, you should send an error message and a 404 header. Sending the header has to be done before any of the content is sent, so a check for whether files exist should be done early in the process.

So, that all in mind, a much better implementation for index.php would be:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM