父目录和 PHP
我真的希望有一个简单的解决方案。
<?php include("header.php"); ?>
假设我的根文件夹中有一个 php 标头,其标题只是 header.php。在该标头中,有一个返回主页的链接 main.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 链接回 main.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与 php 问题相反,这似乎是一个 html 问题。
您的链接应该是带有前面 / ie 的相对链接,
而不是
As opposed to a php prob this seems to be an html prob..
Your links should be relative links with a preceding / i.e.
instead of
使用绝对链接怎么样? header.php 也绝对应该引用 main.php ,那么就不会有问题了:
how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:
您可以使用 base html 标记:
这样您就可以使用该 url 作为基础您所有的链接/样式表/图像,您无需担心它们是否位于子目录中。
You can use the base html tag:
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.
最好的办法是养成使用
这种方式的习惯,这样您就不会混淆您所在的目录等,
因此包含您的标头将非常简单:
the best thing to do is to get in the habit of using
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 :
您可以在 header.php 中硬编码 main.php 的路径:
You could just hard code main.php's path within header.php: