1

I'm trying to make a website and to reuse as much code as possible. I'm using the PHP include statement, but it won't seem to work for me. At the moment I'm only trying to get it to work with the header but I will be doing the same with the nav menu and a footer. I'm only new enough to HTML and have only started learning the include part of PHP today so any help is appreciated. The header file is in an 'includes' folder which is contained in the main website folder. Thanks.

<body>

<?php include"Includes/Header.php";?>

<div class="container-fluid">
    <div id="menu">

            <ul id="menu">
                <li><a href="#Home">Home</a></li>
                <li><a href="#OurProducts">Our Products</a></li>
                <li><a href="#OurBrands">Our Brands</a></li>
                <li><a href="#ContactUs">Contact Us</a></li>
            </ul>

    </div>
</div>

<div id="img1">
    <img src="lasange.jpg" alt="lasange">
</div>

</body>

Header Code

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-   awesome/4.4.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="NewHomepageStyleSheet.css">
    </head>
    <body>
    <div id="toplocation">
        <i class="fa fa-fax"></i>
        (01)-8393790&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<i class="fa fa-map-  marker">&nbsp Unit 10,11,12, Baldoyle Industrial Estate, Dublin 13, Ireland</a>      </i>
    </div>
    </body>
    </html>
Dylan
  • 387
  • 1
  • 13

6 Answers6

0

Try this but make sure this below code must be written in php file:

<body>

<?php include("Includes/Header.php"); ?> // or
<?php require("Includes/Header.php"); ?>

<div class="container-fluid">
    <div id="menu">

            <ul id="menu">
                <li><a href="#Home">Home</a></li>
                <li><a href="#OurProducts">Our Products</a></li>
                <li><a href="#OurBrands">Our Brands</a></li>
                <li><a href="#ContactUs">Contact Us</a></li>
            </ul>

    </div>
</div>

<div id="img1">
    <img src="lasange.jpg" alt="lasange">
</div>

</body>
Vishal Solanki
  • 2,145
  • 2
  • 16
  • 35
0

Because your page requires the header, change include to require. This will force PHP to throw an error when it doesn't find the file (instead of just ignoring and proceeding). The text of the error will be the key

My bet is that your path is not correct. If you use a relative path, it must be relative to the file with the include statement itself, not relative to the main website folder. My advice is to use absolute paths instead.

Finally, while you're in development I suggest that you either enable display errors so you can see error details in the browser, or find out where PHP logs errors so you can check there when something goes wrong.

Putting all of that together, I suggest you replace your include line with:

<?php
    error_reporting(E_ALL);     
    ini_set('display_errors', '1');
    require "$_SERVER[DOCUMENT_ROOT]/Includes/Header.php";    
?>
BeetleJuice
  • 33,709
  • 16
  • 78
  • 137
  • your syntax for document root could be better, but that's the right idea. – Martin Jul 15 '16 at 13:49
  • `require $_SERVER['DOCUMENT_ROOT']."/Includes/Header.php"` Try and avoid including arrays within strings and instead concatenate them. It's no biggie but it's a cleaner, tidier and best practise. As well as giving PHP less work to do. – Martin Jul 15 '16 at 13:52
  • Cleaner and tidier are subjective; I find the other way cleaner, especially when there are lots of variables. Dropping in and out of strings to concatenate with variables seems messy to me. Best practice on the other hand is not subjective and that matters to me. If you could point me to a reference it would be helpful. The official docs make no note about avoiding this syntax (http://php.net/manual/en/language.types.string.php#language.types.string.parsing) – BeetleJuice Jul 15 '16 at 14:01
  • http://stackoverflow.com/a/13665/3536236 will show you there is a significant speed increase to concatenation. Also as well you are quoting an array key without single quotes whic will introduce another speed penalty. This is best practise stuff as stated in the [Zend PHP Best Pactise](https://framework.zend.com/manual/1.10/en/coding-standard.html). – Martin Jul 15 '16 at 14:05
  • The best practice explicitly permits what I did. See the Variable Substitution section of https://framework.zend.com/manual/1.10/en/coding-standard.coding-style.html The performance test you showed me were really helpful though. I was left unmoved by the difference between what I did and what you suggested (only about 1 millisecond gained in 100,000 ops). However, the tests show that single-quoted strings are much faster since PHP doesn't need to worry about parsing: `require $_S['D'].'/path/to/file.php'` is best. Thanks Martin. – BeetleJuice Jul 15 '16 at 14:16
  • To be honest It's been a long time since I read the Zend Best Practise but I previously have tried to stick to it pretty well. As I say your code works fine but I have a mini-obsession that things are faster concatenated `:D` – Martin Jul 15 '16 at 14:22
0

This is a collection of points from the comments, all of which are pretty good:

  • Your syntax could do with having a space: include "file.php";.

  • Your includes should be treated as case sensitive, on LAMP case sensitivity is a standard. It is a good habit to get into.

  • Includes need to run on a PHP page, if your page is HTML it will not include anything. PHP can include any page, it doesn't need to always call a PHP page.

  • Turn on PHP Error logging to check what the reason for the include not displaying is. See PHP Error Displaying.

  • Be sure that your include file does contain something to output to the browser!

  • Includes and requires and their _once counterparts are all practically identical in their way they work (there's a shocking number of folks on this question with answers thinking just changing from include to require will make it work, or changing to include_once will make it suddenly work.

    The difference with require is that if the call fails then PHP ends the script with an error. Hence the contents included is required (high-hat!).

Directories

Includes typically start from your current working PHP directory, so if you have a file at /home/account/public_html/horses/index.php and you are including a file from the root html directory /home/account/public_html/include.php this can not be reached relatively from the current directory which is /horses/.

To sidestep this whole issue above you should use best practise of including files with an absolute file path, such as using $_SERVER['DOCUMENT_ROOT'] if including files which are within the public website area of your account. If including files from other parts of your account then still give an absolute path but manually, (using a DEFINE or suchlike can help) like : /home/account/extras/include.php

A few examples of each scenario:

Relative path:

include "folder/file.php";

Above: The folder needs to be in the same directory as the file running the include.

include "../parentfolder/folder/file.php";

Above: This will not work. Includes can not change relative directories above the current working directory.

include $_SERVER['DOCUMENT_ROOT']."/folder/file.php";
// reference address: /home/account/public_html/folder/file.php

Above: This include will be displayed from any page on any location of your website, as long as the include is withing the HTML Document Root (typically public_html). I recommend using $_SERVER['DOCUMENT_ROOT'] as the standard way or referencing includes.

define("RootHost","/home/account/secret");
include RootHost."/folder/file.php";
// reference address: /home/account/secret/folder/file.php

The above code would typically be used for including content that is not reachable by the website browser such as being not within your public_html HTML document root folder.

Community
  • 1
  • 1
Martin
  • 19,815
  • 6
  • 53
  • 104
0

This should be a comment, but its getting a bit long....

In terms of structure this is messy (this not about your problem but your approach). The HTML head part should not appear inside the body tags, and frequently you'll find you want to modify the HTTP headers - which requires PHP code before any html output.

Although there's a case for putting self-contained fragments in individual include files, these should be well formed (i.e. open and close tags within the same file).

A better approach is to use functions to output structural elements of the content and invoke these from your page script, in addition to ensuring that your HTML is well formed it also protects you against security vulnerabilities introduced by emitting partial content directly from an include file when its URL is typed directly into a browser.

consider:

<?php
define('BASE_PATH', dirname(__FILE__)); // defining as root for your app simplifies includes
include(BASEPATH . '/pageTemplate.php');  
include(BASEPATH . '/menu.php');
?>
<html>
  <head>
   <title>My demo page</title>
   <?php template_required_css_and_js(); ?>
  </head>
  <body>
   <?php template_body_start(); ?>
...
   <?php menu(); ?>
...
   <?php template_body_end(); ?>
  </body>
</html>

(BTW: check '.' is in your php.ini include_path and the permissions on the file allow your webserver to read it. Reading the error log is always a good idea when you have a problem).

Martin
  • 19,815
  • 6
  • 53
  • 104
symcbean
  • 45,607
  • 5
  • 49
  • 83
0

PHP root is not read the same as your standard root. Try replacing your code with this:

include($_SERVER['DOCUMENT_ROOT']."Header.php");
objectively C
  • 732
  • 6
  • 23
  • you should remove the brackets and you *need* to add a slash before the file: `include $_SERVER['DOCUMENT_ROOT']."/Header.php";` – Martin Jul 15 '16 at 18:24
  • This code will work as written. server[document_root] will return "/foldername/" which will make the string /foldername/header.php – objectively C Jul 18 '16 at 14:01
  • Well on my setup for `$_SERVER` the document root does not have a trailing slash, maybe this changes on different servers, but I'd ask you to make sure that the code works? – Martin Jul 18 '16 at 14:28
-1

above Solution is right just small change:

<?php include_once("Includes/Header.php"); ?> // or
<?php require_once("Includes/Header.php"); ?>
Pramod Kharade
  • 1,530
  • 1
  • 13
  • 32