简体   繁体   中英

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.

However i have multiple sub directories with multiple files in each.

Homepage --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. when i include the bar in the sub files, for example banner2.html it wont correctly link.

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

<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

<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). 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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