用于 HTML 的 PHP 包含?

2022-01-25 00:00:00 include php html

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!

解决方案

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

相关文章