dongxing8650 2015-05-09 16:05
浏览 169
已采纳

PHP包含/未定义的变量

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.

  • 写回答

3条回答 默认 最新

  • duanruinong0619 2015-05-09 16:28
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度