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 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)