Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

file hierarchy

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.

like image 255
Vetaxili Avatar asked Jan 20 '12 12:01

Vetaxili


People also ask

HOW include PHP file in another directory?

The way I do it is visual. I put my mouse pointer on the index. php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.

What is include () in PHP?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.

Can I include PHP file from another server?

Nope, this setting is disabled/not allowed by default in most web servers (php. ini) so you can not use the include to include the files from a remote addresss for security reasons.

How do I call a PHP page from another PHP page?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.


2 Answers

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);
?>
like image 91
Herbert Avatar answered Oct 27 '22 00:10

Herbert


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

like image 36
Mcg1978 Avatar answered Oct 27 '22 00:10

Mcg1978