简体   繁体   中英

Include PHP, not working when included php code is for files in different directories

I'm writing quite large website, so I decided to use include functions to make it easier to change the banner, menu, footer etc.

Everything works fine as long as the file is within the same file directory. From the other post I found on 'stackoverflow' I found how to made the path for included file working.

The problem I'm having is that even though the actual web page can find the included file it still doesn't work as it seems to not be able to find the right directory for the actual link.

I'm going to try and use an example code to show what I mean... Let's say it is my file directory

  1. Some Website
  2. [CSS] [articles] [inc] page.php page2.php
  3. page3.php

So if i want to link page.php with page2.php the link would be <a href="page2.php"> But if I want to link page3.php with page2.php the link would be <a href="../page2.php">

And I think this is where the conflict is. That the file directory in menu.php is not working for all the pages.

I would really appreciate any help with this.

EDIT:

I will try and explain a little bit better about the structure of the website. Sorry if I am a bit confusing (I sometimes find it hard to explain exactly what I mean as english is not my first language).

This is example of my website file directory.

The only reason I want to use include function is so that I can use the same banner images, menu navigation or footer across all the web pages.

文件层次结构

Let's say my menu.php (in 'inc folder') which would be included in all the pages looks like that

<ul>
<li><a href="index.php"  class="navon">Home Page</a></li>
<li><a href="contact.php">Contact us</a></li>
</ul>

When I use

<?php include ("inc/menu.php"); ?>

(or any appropriately directed file)

everything appears the way I want it to be, but the problem is that when I press index.php or any other link to a web page from a different directory, it doesn't work.

Your question has a few mistakes and issues with formatting so I may be picking it up wrong. But you need to include the path to your included files otherwise php won't know where to look.

For example, if have page2.php in the inc folder and you want to include page2.php in page3.php you'd use something like this:

include('inc/page2.php');

if you wanted to include page3.php inside page2.php it would be something like this:

include('../page3.php');

You can also use the full path to your include file, something like this:

include('/home/user/vhosts/yoursite/public_html/inc/page2.php');

Although I'd recommend creating some sort of config file to set the base folder of your site in one place and then re-use it where you need.

This is pretty basic php stuff so I'd suggest following a few tutorials first before jumping into building a site like this. Google is your friend, but here's a start: http://php.net/manual/en/tutorial.php

The problem: The links in your menu are relative to the pages in which menu.php is included. For example, including menu.php in art1.php makes the link to contact.php point to http://yoursite.com/articles/contact.php rather than http://yoursite.com/contact.php .

Solution: Use absolute URLs in your menu links:

<ul>
    <li><a href="http://yoursite.com/index.php"  class="navon">Home Page</a></li>
    <li><a href="http://yoursite.com/contact.php">Contact us</a></li>
</ul>

One way to highlight the current page is to do something like this in menu.php:

<?php
// DO NOT CHANGE THESE
$server = 'http://'.$_SERVER["SERVER_NAME"];    // domain name or localhost
$this_page = $server . $_SERVER["PHP_SELF"];    // name of the script that
                                                //  includes menu.php

/*
 * This array holds the menu items. I put in a few examples.
 * 
 * The key (left side/before =>) is the URL to a page.
 * It takes the form "$server/path/to/somepage.php"
 * Make sure to include $server at the beginning.
 * 
 * The value (right side/after =>) is the text that will appear in the menu.
 *
 * This is the only part you need to change to fit your site.
 * Make sure you put a comma after each item, except the last one.
 */
$menuitems = array(
    "$server/index.php"                   => 'Home Page',  // notice the
    "$server/home_page/page1.php"         => 'Page 1',     // comma after
    "$server/home_page/articles/art1.php" => 'Article 1',  // each item
    "$server/contact.php"                 => 'Contact Us'  // except the last
);


// The rest just generates an unordered list in HTML
// You do not need to change anything below.

$doc = new DOMDocument();
$menu = $doc->createElement('ul');
$doc->appendChild($menu);

// this creates the list of links in your menu
foreach ($menuitems as $url=>$label) {
    $item = $doc->createElement('li');
    $menu->appendChild($item);

    $link = $doc->createElement('a',$label);
    $link->setAttribute('href', $url);

    // if the current page matches the current link,
    // set class="current" on that link
    if ($url === $this_page)
        $link->setAttribute('class', 'current');

    $item->appendChild($link);
}

echo $doc->saveHTML($menu);
?>

Use an initial gateway php file, lets say you're structure looks like this:

folders:

/downloads
/templates
/templates/my_design
/templates/my_design/css
/templates/my_design/js
/system

So what you need is an entry point for every page you're calling:

/index.php

In this file you link your system config file, lets say:

require_once '/system/config.inc.php'

Now in the config.inc.php you set a constant for your root:

define('PAGE_ROOT','/home/user/vhosts/example.com/httpdocs/');

Now you can eveywhere in your page link includes with this constant as following:

include PAGE_ROOT.'/templates/my_design/theme.php';

Now for your subpages you can use two different approches:

First , Working with parameters

index.php?section=guestbook

In the index.php file you parse the parameters so you know which php file you have to include.

Second , mod_rewrite

With mod_rewrite you can also lead every page call to your index.php

RewriteEngine On
RewriteRule .* index.php
RewriteBase /

example.com/section/guestbook

Here you set a class, lets say urlworker(); which splits the requested uri into the different parts, so you know again which site you have to include.

使用绝对路径

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

In PHP the included files will appear to be part of the file they are included into. So if you have images and links in page2.php and include page2 into page1.php then all your images and links will appear to be relative to page1.php. So page2.php is in a subfolder and you have images for page2 in that same subfolder, when you include it into page1.php your image sources will be wrong.

One way around this is to have all your image sources and site links be relative to the root instead of relative to the php file. So design your sources and links starting with the root folder, "/" if you're site is at the top of the domain or "/sometopfolder" if the site starts in a subfolder, and then go from there.

might I suggest you use a framework or create your own basic MVC (framework)

Then you have better control over what gets loaded where and how

how to write classes here and here

If your website is going to be 'big' start as you mean to go on and program it properly from the start... look at OOP and MVC - it will help you in the long run.

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