【问题标题】:PHP include / undefined variablePHP 包含/未定义的变量
【发布时间】:2015-05-09 16:05:28
【问题描述】:

我知道有更多关于这个问题的主题,我已经阅读了它们,但我没有找到答案。

我有一个 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' 等,但我收到“未定义变量”错误“未定义变量:C 中的 v:\ wamp\www\test\index.php 第 39 行"

如果我在 index.php 中定义变量 $v,它可以工作,但这意味着我必须创建 index.php 的副本,这有点违背了目的。我只希望在 index.php 中包含 pag1.php、pag2.php、pag3.php 等,而不是创建大量副本。我可以为此使用 html :)

我知道我可能会问一个愚蠢的问题,但我是 php 的新手,我不知道在哪里寻找答案。提前谢谢你。

【问题讨论】:

    标签: php variables undefined php-include


    【解决方案1】:

    严格来说,你只需要改变

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

    【讨论】:

    • 还要感谢 James K,我完全没有意识到安全风险。我想我有很多东西要读。
    【解决方案2】:

    你需要在调用之前定义$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

    【讨论】:

    • 谢谢你,北极星。 (老实说,我认为从 navbar.php 调用 v 就足够了。就像我说的,我是新手。
    【解决方案3】:

    要在 URL 中传递变量,您需要使用 $_GET 数组。它会在你的脚本运行之前自动填充,所以你只需要从中读取。例如,$_GET['v'] 将包含您通过 URL (?v=...) 传入的值。

    请注意,最好先检查该值是否已设置,否则它会向您抛出一个令人讨厌的错误,以防万一。以下代码是处理未设置参数的简单方法:

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

    希望这能解决问题。

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2015-08-29
      • 1970-01-01
      • 2011-09-19
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2014-01-28
      相关资源
      最近更新 更多