父目录和 PHP

发布于 2024-11-28 22:54:05 字数 1043 浏览 1 评论 0原文

我真的希望有一个简单的解决方案。

<?php include("header.php"); ?>

假设我的根文件夹中有一个 php 标头,其标题只是 header.php。在该标头中,有一个返回主页的链接 ma​​in.php,并且 main.php 也位于根目录中。到目前为止没有问题。这是 header.php 的样子。简单的事情,对吧?

<link href="style.css" rel="stylesheet" type="text/css" />

<div id="headerwrap">

        <div id="linkbox">
            <a href="main.php"><img src="images/mainlogo.png" /></a>
        </div><!-- End linkbox -->

</div>

但是,假设我的子目录中有其他页面。 Subpage.php 位于根目录的子目录中,因此必须返回根目录才能获取包含的 header.php

<?php include("../header.php"); ?>

这没什么大不了的,除了 header.php 链接回 ma​​in.php 以及样式表,而这些样式表都不在*子页面中。 php 的目录,因此当子页面上的某人尝试通过标头中的链接返回主页面时会导致错误。

我只是希望有一种简单的方法来完成这项工作,并且我不必将所有包含内容复制并重定向到每个子目录中。此外,页面太多,无法真正合理地将它们全部包含在根目录中。抱歉,如果这个答案发布在其他地方;我已经看过了,但并不真正知道我在寻找什么。感谢您的帮助。希望这一切都是有意义的。

I really hope there's a simple solution to this.

<?php include("header.php"); ?>

Let's say I have a php header in my root folder simply titled header.php. In that header there is a link back to the home page, main.php, and main.php is also located on the root. No problem so far. Here's what header.php looks like. Simple stuff, right?

<link href="style.css" rel="stylesheet" type="text/css" />

<div id="headerwrap">

        <div id="linkbox">
            <a href="main.php"><img src="images/mainlogo.png" /></a>
        </div><!-- End linkbox -->

</div>

However, let's say I have other pages in subdirectories. Subpage.php is located in a child directory of the root, and so it has to look back to the root to get the included header.php.

<?php include("../header.php"); ?>

That wouldn't be a big deal, except that header.php links back to main.php and also style sheets, none of which are in *subpage.php's directory, thus causing an error when someone on Subpage tries to get back to Main via the link in the header.

I'm just hoping there's a simple way to make this work, and that I don't have to copy and redirect all includes into every subdirectory. Also, there are too many pages to really reasonably include them all on the root. Sorry if this answer is posted elsewhere; I've looked and just have no real idea what I'm looking for. Thanks for your help. Hope all that makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹉夏雨初晴づ 2024-12-05 22:54:06

与 php 问题相反,这似乎是一个 html 问题。

您的链接应该是带有前面 / ie 的相对链接,

<a href="/main.php"> Text </a>

而不是

<a href="main.php"> Text </a>

As opposed to a php prob this seems to be an html prob..

Your links should be relative links with a preceding / i.e.

<a href="/main.php"> Text </a>

instead of

<a href="main.php"> Text </a>
何其悲哀 2024-12-05 22:54:06

使用绝对链接怎么样? header.php 也绝对应该引用 main.php ,那么就不会有问题了:

<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>

how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:

<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>
像极了他 2024-12-05 22:54:06

您可以使用 base html 标记:

<base href="http://yoursite.com/" />

这样您就可以使用该 url 作为基础您所有的链接/样式表/图像,您无需担心它们是否位于子目录中。

You can use the base html tag:

<base href="http://yoursite.com/" />

This way you can use that url as the base for all your links/stylesheets/images and you don't need to worry if they're in a subdirectory.

似狗非友 2024-12-05 22:54:06

最好的办法是养成使用

$_SERVER['DOCUMENT_ROOT']

这种方式的习惯,这样您就不会混淆您所在的目录等,

因此包含您的标头将非常简单:

include $_SERVER['DOCUMENT_ROOT'] . "/header.php";

the best thing to do is to get in the habit of using

$_SERVER['DOCUMENT_ROOT']

that way you have no confusion as to what directory you're in, etc.

so including your header for example would be as simple as :

include $_SERVER['DOCUMENT_ROOT'] . "/header.php";
远昼 2024-12-05 22:54:05

您可以在 header.php 中硬编码 main.php 的路径:

<a href="http://website.com/main.php"><img src="http://website.com/images/mainlogo.png" /></a>

You could just hard code main.php's path within header.php:

<a href="http://website.com/main.php"><img src="http://website.com/images/mainlogo.png" /></a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文