28

I have a navigational bar, an image, and a heading that I'll be including in every page of my website, so I wanted to use php include to refer to this code in several pages. However, I think I may have the syntax wrong or something because it's not rendering anything when I load it. Here are some code snippets:

<!-- sample page --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html>  
<head>  
<?php include ('headings.php'); ?>
</head>
<body>
<?php include ('navbar.php'); ?>
<?php include ('image.php'); ?> 
</body>
</html>

navbar.php

    <?php 

    echo '<ul id="nav">
        <li>
            <a href="Home.html">Home</a>
        </li>
        <li>
            <a>About Me</a>
            <ul>
                <li>
                    <a href="Career.html">Career</a>
                </li>
                <li>
                    <a href="Coding.html">Coding</a>
                </li>
                <li>
                    <a href="Personal.html">Personal</a>
                </li>
            </ul>
        </li>
        <li>
            <a href="Travels.html">Travel</a>
        </li>
        <li>
            <a href="Contact.html">Contact</a>
        </li>
    </ul>';

    ?>

Thanks for helping!

13
  • 8
    There's no reason to have this in a PHP tag and use echo. You can include it even if it's just raw HTML. Edit: That said, your code looks fine. Have you tried to view the source, and could you post it in the OP? Edit: Perhaps it's because PHP isn't installed on your server, and therefore your browser just renders the PHP tags as regular HTML tags (nothing).
    – h2ooooooo
    Aug 9, 2012 at 16:05
  • +1 like you did for the head and body tags
    – Waygood
    Aug 9, 2012 at 16:07
  • You might want to consider a template engine instead. Check out Smarty, Twig, or Dwoo as examples. There are a ton more out there.
    – WWW
    Aug 9, 2012 at 16:09
  • Can you view the source of the page when you load it? That might deem you with some insight of what actually happens. Aug 9, 2012 at 16:10
  • 1
    Yes it has to be .php. That is actually included in 2 of the 3 answers below.
    – Vatev
    Aug 9, 2012 at 16:27

8 Answers 8

30

You can use php code in files with extension .php and only there (iff other is not defined in your server settings).

Just rename your file *.html to *.php


If you want to allow php code processing in files of different format, you have two options to do that:

1) Modifying httpd.conf to allow this for all projects on your server, by adding:

AddHandler application/x-httpd-php .htm .html

2) Creating .htaccess file in your separate project top directory with:

<Files />
    AddType application/x-httpd-php .html
</Files>

For second option you need to allow use of .htaccess files in your httpd.conf, by adding the following settings:

AllowOverride All
AccessFileName .htaccess

*that is correct for Apache HTTP Server

16
  • 10
    False. As long as the server is set up to handle different extensions as PHP files, you can include PHP code in a file with ANY extension (i.e. .js, .css, .html, etc.).
    – Matt
    Aug 9, 2012 at 16:08
  • You can use PHP in files with other extensions as well.
    – jprofitt
    Aug 9, 2012 at 16:08
  • 2
    Okay, iff you set that option in your server settings. Aug 9, 2012 at 16:09
  • 11
    Most shared servers are only set up to handle php in .php files. Yes, you can have it set up otherwise, but come on -- someone who's asking a question at this level probably hasn't gotten that far in figuring out how to do this. Aug 9, 2012 at 16:10
  • 1
    @EdwardRuchevits With include/require it doesn't matter what the filename is. It is always executed as PHP.
    – Vatev
    Aug 9, 2012 at 16:11
11

You don't need to be echoing the info within the php file. A php include will automatically include any HTML within that file.

Make sure you're actually using a index file with a .php extension, .html won't work with php includes. (Unless you're telling your server to treat .html files otherwise)

Make sure your paths are correctly set up. From your description, the way you've set it up your header.php/navbar.php/image.php files should be in your root directory. So your root directory should look like this:

index.php
navbar.php
image.php
header.php

Otherwise if those PHP files are in a folder called /includes/, it should look like so:

<?php include ('includes/headings.php'); ?>

4

Try to get some debugging information, could be that the file path is wrong, for example.

Try these two things:- Add this line to the top of your sample page:

<?php error_reporting(E_ALL);?>

This will print all errors/warnings/notices in the page so if there is any problem you get a text message describing it instead of a blank page

Additionally you can change include() to require()

<?php require ('headings.php'); ?>
<?php require ('navbar.php'); ?>
<?php require ('image.php'); ?>

This will throw a FATAL error PHP is unable to load required pages, and should help you in getting better tracing what is going wrong..

You can post the error descriptions here, if you get any, and you are unable to figure out what it means..

1
  • Thanks, but we got it figured out. I'm sure this will be helpful in the future, though!
    – sresht
    Aug 9, 2012 at 16:35
2

First: what the others said. Forget the echo statement and just write your navbar.php as a regular HTML file.

Second: your include paths are probably messed up. To make sure you include files that are in the same directory as the current file, use __DIR__:

include __DIR__.'/navbar.php'; // PHP 5.3 and later
include dirname(__FILE__).'/navbar.php'; // PHP 5.2
2

When the use case is that only include a html page as output, php native method readfile() should be called instead. This method simply reads a file and writes it to the output buffer.

Usage:

readfile(__DIR__ . '/../public/react.html');
exit();
1

Here is the step by step process to include php code in html file ( Tested )

If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."

You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.

First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file. (Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess") Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file: AddType application/x-httpd-php .html .htm If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5: AddType application/x-httpd-php5 .html .htm Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.

You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
     <HTML>
 <HEAD>
 <TITLE>Use PHP in HTML files</TITLE>
 </HEAD>

   <BODY>
    <h1>
    <?php echo "It works!"; ?>
    </h1>
  </BODY>
 </HTML>

Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...) If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.

1

I figured out a simple solution to the PHP include stuff. Simply rename all your .html files to .php and your're good to go.

0

I have a similar issue. It appears that PHP does not like php code inside included file. In your case solution is quite simple. Remove php code from navbar.php, simply leave plain HTML in it and it will work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.