【问题标题】:PHP NavBar works on its own but not when included in other PHP codePHP NavBar 独立工作,但在包含在其他 PHP 代码中时不起作用
【发布时间】:2022-08-20 04:26:26
【问题描述】:

我正在处理我的 PHP 代码中的小问题,当我尝试在我的模拟网站上使用 include 以在我的所有页面上拥有相同的导航栏时,它根本不起作用,但是它在我自己打开。

我对编码很陌生,所以请告诉我,我完全缺乏适当的术语。

这是我的 index.php :

<!DOCTYPE html>
<html lang=\"en\">
<head>
    <meta charset=\"utf-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
    <title>T-drip Home</title>
    <link rel=\"stylesheet\" href=\"css/home.css\">
    <?php include(\'inc/nav.php\') ?>
</head>
<body>
    <div class=\"fadeIn\">
        <img src=\"images/home-white-tee-1.jpg\" alt=\"Fancy 1st Tee\">
        <img src=\"images/home-white-tee-2.jpg\" alt=\"Provocative 2nd Tee\">
        <img src=\"images/home-white-tee-3.jpg\" alt=\"Classy 3rd Tee\">
    </div>
    <section class=\"prod\">
        <h3 class=\"prodCat\">Always Popular</h3>
        <div class=\"prodCont\">
        <!-- first product -->
            <div class=\"prodCard\">
                <div class=\"prodJpg\">
                    <img src=\"images/white-tee-1.jpg\" class=\"prodTh\" alt=\"Trendy, over-designed white T-shirt\">
                    <button class=\"jpgBtn\">Add to cart</button>
                </div>
                <div class=\"prodInfo\">
                <h4 class=\"prodName\">Slightly<span style=\"color: #f28816\"> White T-Shirt</span></h4>
                <span class=\"price\">£566.99</span>
                </div>
            </div>
            <!-- second product-->
            <div class=\"prodCard\">
                <div class=\"prodJpg\">
                    <img src=\"images/white-tee-2.jpg\" class=\"prodTh\" alt=\"Common Mans White T-Shirt\">
                    <button class=\"jpgBtn\">Add to cart</button>
                </div>
                <div class=\"prodInfo\">
                <h4 class=\"prodName\">Very<span style=\"color: #f28816\"> White T-Shirt</span></h4>
                <span class=\"price\">£364.99</span>
                </div>
            </div>
        </div>
    </section>
    <!-- js is needed for the aforementioned fade gallery -->
    <script type=\"text/javascript\" src=\"inc/script.js\">
    </script>

</body>
</html>

Index.php 可以正常工作,但 &lt;?php include(\'inc/nav.php\') ?&gt; 行除外 这根本不起作用,就好像该代码一开始就不存在一样。

这是我的 nav.php :

<nav class=\"navBar\">
    <link rel=\"stylesheet\" href=\"../css/nav.css\">
    <div class=\"nav\">
        <h1 class=\"logo\"> T-<span style=\"color: #f28816\">Drip</span><br>Online<span style=\"color: #f28816\"> Store</span></h1>
        <div class=\"profBas\">
            <a href=\"../cart.php\"><img src=\"../images/cart.png\" alt=\"cart\"></a>
        </div>
    </div>
    <ul class=\"linkCont\">
        <li class=\"linkItm\"><a href=\"../index.php\" class=\"link\">home</a></li>
        <li class=\"linkItm\"><a href=\"../about.php\" class=\"link\">about</a></li>
        <li class=\"linkItm\"><a href=\"../store.php\" class=\"link\">store</a></li>
    </ul>
</nav>

还有一个包含所有文件的文件夹的屏幕截图,以防万一:

ss of the folder

我已经在这个问题上拉了很长时间了,我非常感谢你在这件事上的帮助。

编辑1:

这些文件目前不在任何服务器上,我只是使用带有 Web 开发人员扩展程序的 Firefox 浏览器查看它们。 我应该早点说,对不起。

我已按照建议将include 行移至&lt;body&gt;,但这并没有帮助。 (我将把它留在那里等待所有其他建议的修复,因为它应该是从它开始的地方)。

我也试过:

ini_set( \'error_reporting\', E_ALL );
ini_set( \'display_errors\', true );

include \'./members/reg.php\';

<?php
ini_set( \'error_reporting\', E_ALL );
ini_set( \'display_errors\', true );

include \'./members/reg.php\';
?>

但这些都没有帮助我。

另外澄清一下,无论我在代码中是否有include 行,当我查看文件时都没有任何变化。


好吧,我放弃了,我不明白为什么包含不能正常工作。 我只是将导航复制粘贴到每个页面中,但感谢您的帮助,我很感激。

  • 这是您机器上的本地网站还是在托管服务提供商处设置?我问的原因是你可能会收到一个错误,但它们在生产中被抑制了,所以你看不到它。如果它不是致命的,我认为缺少的包含是警告那你就看不到了。另外,请参阅下面的答案。可能是包含工作正常,并且由于在&lt;head&gt; 标记中放置不当而将代码剥离。尝试将其移至&lt;body&gt; 标签。
  • 这回答了你的问题了吗? php include doesn\'t work even inside the same folder
  • @War10ck 但是,错误仍将出现在日志中。
  • @马丁是的。看的好地方。

标签: php html include


【解决方案1】:

第一个原因可能是代码中的导航栏位于&lt;head&gt; 标记内,而该标记应位于&lt;body&gt; 标记之外。首先这样做,然后我们找出下一个原因。

【讨论】:

  • 感谢您的留言,我已将 &lt;?php include('inc/nav.php') ?&gt; 移至 &lt;body&gt; 标签,但这并没有改变任何东西,我也尝试将它放在头部和身体之间,但也没有任何作用
  • @RobotDog 什么都不应该是之间&lt;/head&gt;&lt;body&gt;
  • 您能说出 Navbar 中的问题是什么,您想要显示的内容不清楚吗?
  • 好吧,不仅导航栏没有显示,而且无论我在代码中是否有include 行,网站的外观似乎都没有改变。
  • 哦,每当我在 index.php 中用它替换 include 行时,来自 nav.php 的代码工作得非常好(当然对文件路径进行了小改动)
【解决方案2】:
  • 正如@Ramkrishna's answer 中正确说明的那样,HTML &lt;nav&gt; 元素应该在 HTML &lt;body&gt; 标记内。不是&lt;head&gt;

  • 至于不显示nav.php文件;您需要检查您的文件路径是否正确。 directory_containing_PHP_HTMLfile/inc/nav.php 存在吗?它可读吗?

  • 最佳实践是从服务器文档根目录(通常是服务器的 Web 可访问部分的基本目录)中为包含提供一个绝对文件路径

笔记:$_SERVER['DOCUMENT_ROOT'] 仅在本地(主机)机器而不是服务器机器上运行时将不起作用。 Use an alternative

  • 另外,includerequire不是需要括号。

总而言之,这让我们:

<html>
    <head>...</head>
    <body>
        <?php 
        include $_SERVER['DOCUMENT_ROOT']."/inc/nav.php";
        ?>
    </body>
</html>

最后你应该Read your PHP Error logs 告诉你包含的文件是否有问题。

【讨论】:

  • 感谢您的回复@Martin,我尝试实现建议的代码,但遗憾的是它没有帮助。我现在正在调查错误日志问题,但可能需要一些时间,因为我还不熟悉所使用的术语。
  • @RobotDog 抱歉,我意识到 $_SERVER['DOCUMENT_ROOT'] 仅在本地机器而不是服务器机器上运行时并不总是有效。建议您从代码中删除该变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 2014-05-15
  • 1970-01-01
相关资源
最近更新 更多