包含PHP,当包含php代码用于不同目录中的文件时不工作

Vet*_*ili 5 php

我正在写一个相当大的网站,所以我决定使用包含功能,以便更容易更改横幅,菜单,页脚等.

只要文件位于同一文件目录中,一切正常.从我在'stackoverflow'上找到的另一篇文章中,我发现了如何使包含文件的路径工作.

我遇到的问题是,即使实际的网页可以找到包含的文件,它仍然无法正常工作,因为它似乎无法找到实际链接的正确目录.

我将尝试使用示例代码来显示我的意思...让我们说它是我的文件目录

  1. 一些网站
  2. [CSS] [articles] [inc] page.php page2.php
  3. page3.php

因此,如果我想链接page.php与page2.php链接将是<a href="page2.php"> 但如果我想链接page3.php与page2.php链接将是<a href="../page2.php">

我认为这就是冲突所在.menu.php中的文件目录不适用于所有页面.

我真的很感激任何帮助.

编辑:

我会尝试更好地解释一下网站的结构.对不起,如果我有点困惑(我有时很难解释我的意思,因为英语不是我的第一语言).

这是我的网站文件目录的示例.

我想使用包含功能的唯一原因是我可以在所有网页上使用相同的横幅图像,菜单导航或页脚.

文件层次结构

假设我的menu.php(在'inc folder'中)将包含在所有页面中,就像那样

<ul>
<li><a href="index.php"  class="navon">Home Page</a></li>
<li><a href="contact.php">Contact us</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我用的时候

<?php include ("inc/menu.php"); ?>
Run Code Online (Sandbox Code Playgroud)

(或任何适当指示的文件)

一切都以我想要的方式出现,但问题是,当我从另一个目录按index.php或任何其他链接到网页时,它不起作用.

Her*_*ert 5

问题:菜单中的链接与包含 menu.php 的页面相关。例如,在 art1.php 中包含 menu.php 会使 contact.php 的链接指向http://yoursite.com/articles/contact.php而不是http://yoursite.com/contact.php

解决方案:在菜单链接中使用绝对 URL:

<ul>
    <li><a href="http://yoursite.com/index.php"  class="navon">Home Page</a></li>
    <li><a href="http://yoursite.com/contact.php">Contact us</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

突出显示当前页面的一种方法是在 menu.php 中执行以下操作:

<?php
// DO NOT CHANGE THESE
$server = 'http://'.$_SERVER["SERVER_NAME"];    // domain name or localhost
$this_page = $server . $_SERVER["PHP_SELF"];    // name of the script that
                                                //  includes menu.php

/*
 * This array holds the menu items. I put in a few examples.
 * 
 * The key (left side/before =>) is the URL to a page.
 * It takes the form "$server/path/to/somepage.php"
 * Make sure to include $server at the beginning.
 * 
 * The value (right side/after =>) is the text that will appear in the menu.
 *
 * This is the only part you need to change to fit your site.
 * Make sure you put a comma after each item, except the last one.
 */
$menuitems = array(
    "$server/index.php"                   => 'Home Page',  // notice the
    "$server/home_page/page1.php"         => 'Page 1',     // comma after
    "$server/home_page/articles/art1.php" => 'Article 1',  // each item
    "$server/contact.php"                 => 'Contact Us'  // except the last
);


// The rest just generates an unordered list in HTML
// You do not need to change anything below.

$doc = new DOMDocument();
$menu = $doc->createElement('ul');
$doc->appendChild($menu);

// this creates the list of links in your menu
foreach ($menuitems as $url=>$label) {
    $item = $doc->createElement('li');
    $menu->appendChild($item);

    $link = $doc->createElement('a',$label);
    $link->setAttribute('href', $url);

    // if the current page matches the current link,
    // set class="current" on that link
    if ($url === $this_page)
        $link->setAttribute('class', 'current');

    $item->appendChild($link);
}

echo $doc->saveHTML($menu);
?>
Run Code Online (Sandbox Code Playgroud)