简体   繁体   中英

Include html pages in a php file

I would like to create a template using html and css so I created the following template.html file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Application</title>
<link href="CSS/layout.css" rel="stylesheet" />
<script src=""></script>
</head>

<body>
<div class="wrapper">
    <div class="header">
        <!--<div class="header_left"></div>
        <div class="header_right"> -->
            <h3>Application</h3>
            <h4>****  Project ****</h4>

    </div>
    <div class="navbar">
        <ul class="mainnavbar">
            <li><a href="#">Home</a></li>
            <li><a href="login.html">About</a></li>
            <li><a href="#">Description</a></li>
            <li><a href="#">Contact Me</a></li>
        </ul>
    </div>
    <div class="mainbody">
        <div class="leftcol">
            <div class="left_navbar">
                <ul class="left_inner">
                    <li><a href="#">Login</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Description</a></li>
                    <li><a href="#">Contact me</a></li>
                </ul>
            </div>
        </div>
        <div class="midcol">
            Center
        </div>
        <div class="rightcol">
            Right Column
        </div>
    </div>
    <div class="footer">Designed By <a href="">Me</a></div>
</div>
</body>
</html>

Because I want to create a dynamic website I want to separate this template.html into index.php, header.php, navbar.php, home.php and footer.php. So I did the following:

index.php

<?php
include("includes/header.html");
include("includes/navbar.html");
include("includes/home.html");
include("includes/footer.html");
?>

header.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Application</title>
<link href="CSS/layout.css" rel="stylesheet" />
<script src=""></script>
</head>
<body>
<div class="wrapper">
    <div class="header">
        <!--<div class="header_left"></div>
        <div class="header_right"> -->
        <h3>Application</h3>
        <h4>****Project ****</h4>
    </div>

navbar.html

<div class="navbar">
<ul class="mainnavbar">
    <li><a href="#">Home</a></li>
    <li><a href="login.html">About</a></li>
    <li><a href="#">Description</a></li>
    <li><a href="#">Contact Me</a></li>
</ul>
</div>

home.html

<div class="mainbody">
<div class="leftcol">
    <div class="left_navbar">
        <ul class="left_inner">
            <li><a href="#">Login</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Description</a></li>
            <li><a href="#">Contact me</a></li>
        </ul>
    </div>
</div>
<div class="midcol">
    Center
</div>
<div class="rightcol">
    Right Column
</div>
</div>

footer.html

<div class="footer">Designed By <a href="">me</a></div>
</div>
</body>
</html>

I have these files in a folder called includes. I just separated the file template.html in 4 html files and then called them from index.php, but I just get a white page when I run index.php in the browsers. Is there something missing?

Your code (syntax and idea with including html) is fine. That should work.

You have to search for problems somewhere else.

Maybe you have a problem with:

  • web server configuration (virtual hosts, directories etc.)
  • wrong url / wrong server (refreshing some web server url, but you work on local XAMPP)
  • file permissions (however apache normally should report error in that case)

Looking at web server error logs may help.

Make sure you aren't just running your template.html just by opening it locally in a browser.

Since PHP is a server side language , you would need to run the page on a live server on a local server such as Apache . WAMP is also a nice tool when locally developing sites that require PHP.

You can also use file_get_contents("path_to_file/file.html") instead of include()

But only if there is HTML content, not PHP

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