如何使用GET & &;动态包含diff目录中的PHP文件


How to use GET & Include to dynamically PHP files from diff directory

我是PHP新手,刚开始学习。需要代码方面的帮助。我已经组织了我的php页面,包括在diff文件夹-内容&安全。现在我想在PHP中使用GET和include函数来包含这些文件夹中的页面。

下面是我包含文件的代码:它只适用于内容目录。请让我什么我需要改变在索引。php代码和我的菜单。php

index . php代码:

include "/templates/header.php";
include "templates/menu.php";
include "/templates/splash.php";
// Set the default name 
$action = "home_index"; 
// Specify some disallowed paths 
$disallowed_paths = array('header', 'menu', 'splash', 'bottom_page', 'footer'); 
if (!empty($_GET['action'])) 
{ 
    $tmp_action = basename($_GET['action']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists ("content/{$tmp_action}.php"))
        $action = $tmp_action; 
} 
// Include $action
include "/content/$action.php";  
include "/templates/bottom_page.php";
include "/templates/footer.php";
?>
下面是我的menu.php代码:

<ul>
            <li class="first current_page_item"><a href="?action=home_index">Homepage</a></li>
            <li><a href="?action=products" title="Products">Products</a></li>
            <li><a href="?action=services" title="Services">Services</a></li>
            '<li><a href="?action=about" title="About Us">About</a></li>
            <li><a href="?action=contact" title="Contact Us">Contact</a></li>
            <?php
            if(isset($_SESSION['username']) && $_SESSION['username']== true){
             echo '<li> <a href="?action=logout">Logout</a></li>';
            }else{
             echo '<li class="last"><a href="?action=login" title="Login Page">Login</a></li>';
             }
             ?>
        </ul>
include "/content/$action.php";  
         ^---

include()在文件系统级别工作,并且绝对不知道您站点的文档根是什么。由于包含路径以/开头,PHP从服务器文件系统的根目录开始搜索文件,而不是站点的根目录。

。你做的相当于

include c:'content'$action

而不是

include c:'path'to'website'document'root'content'$action