简体   繁体   English

使用php include包含多个子目录

[英]Using php include with multiple sub directories

Hello i recently just got my web server to run all .html files as .php files which now allows me to use the php include function, which is very useful. 您好,我最近刚让我的Web服务器将所有.html文件作为.php文件运行,现在允许我使用php include函数,这非常有用。

However i have multiple sub directories with multiple files in each. 但是我有多个子目录,每个子目录中都有多个文件。

Homepage --banners --banner2.html --banner2.html --signs --sign1.html 主页--banners --banner2.html --banner2.html --signs --sign1.html

and so on. 等等。 The problem here is im trying to use include for my top nav bar. 这里的问题是我试图将include用作我的顶级导航栏。 when i include the bar in the sub files, for example banner2.html it wont correctly link. 当我在子文件中包含该栏时,例如banner2.html,它将无法正确链接。

nav.php nav.php

<ul class="nav navbar-nav">
                    <li>
                        <a href="about.html">About</a>
                    </li>
                    <li>
                        <a href="Services.html">Services</a>
                    </li>
                    <li>
                        <a href="index.php">Contact</a>
                    </li>

                    <li>
                        <a href="Login/Sign-In.html">Login</a>
                    </li>

                    <li>
                        <a href="Login/logout.php">Logout</a>
                    </li>

                    <li>
                        <a href="PHP/Cart.php"><img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart</a>


                    </li>

                </ul>

in the nav bar is a link to about.html normally i would do the 在导航栏中是指向about.html的链接,通常我会

<a href="../about.html">about</a>

however i cant do that when every file is going to share the same navigation bar. 但是,当每个文件要共享相同的导航栏时,我无法做到这一点。 How can I fix this? 我怎样才能解决这个问题?

I have answered my own quest and feel silly for even asking the question. 我已经回答了自己的问题,甚至问这个问题也感到很傻。 I just used absoulte paths 我只是用绝对路径

nav.php nav.php

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="http://domain.com/about.html">About</a>
                    </li>
                    <li>
                        <a href="http://domain.com/Services.html">Services</a>
                    </li>
                    <li>
                        <a href="http://domain.com/index.php">Contact</a>
                    </li>

                    <li>
                        <a href="http://domain.com/Login/Sign-In.html">Login</a>
                    </li>

                    <li>
                        <a href="http://domain.com/Login/logout.php">Logout</a>
                    </li>

                    <li>
                        <a href="http://domain.com/PHP/Cart.php"><img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart</a>


                    </li>

                </ul>

There are some ways to fix this, one of them is: 有一些方法可以解决此问题,其中一种是:

Use $_SERVER["DOCUMENT_ROOT"] – We can use this variable to make all our includes relative to the server root directory, instead of the current working directory(script's directory). 使用$_SERVER["DOCUMENT_ROOT"] –我们可以使用此变量来使所有包含都相对于服务器根目录而不是当前工作目录(脚本目录)。 Then we would use something like this for all our includes: 然后,我们将对所有包含项使用类似的内容:

`<a href="<?php echo $_SERVER["DOCUMENT_ROOT"].'/path/to/about.html' ?>">about</a>`

Also you should check this post . 你也应该检查这个帖子

You could use the relative path to the root directory which is /file.ext 您可以使用根目录的相对路径/file.ext

Example: <a href="/about.html">About</a> 示例: <a href="/about.html">About</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM