Page 1 of 1

Including an html file

Posted: Thu Aug 11, 2011 5:34 pm
by The_Mexican
Hi I'm a beginner to php and html and I can't figure out why my code isn't working. This is what I have:

index.php:

Code: Select all

<?php include 'header.html'; ?>
content
<?php include 'footer.html'; ?>
header.html:

Code: Select all

<html>
	<head>
                 content
        </head>
        <body>
        content
footer.html:

Code: Select all

content
          </body>
</html>
I'm pretty sure it has something to do with by code in index.php but I don't know what. Any help is appreciated.

EDIT: Sorry I wasn't clear earlier. The problem is that when I run index.php in firefox it doesn't show the header or footer. The include statements aren't working.

By 'content' in the header I meant the title and things like that.

Re: Including an html file

Posted: Thu Aug 11, 2011 9:23 pm
by califdon
What does "isn't working" mean??? You need to tell us what you expect your code to produce and what is actually produced. Are there any error messages? What is displayed? What do you see when you view the source page in your browser?

Why would you place the 'content' in the header???

Re: Including an html file

Posted: Fri Aug 12, 2011 11:14 am
by flying_circus

Code: Select all

<?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);

  php include 'header.html';
?>
content
<?php include 'footer.html'; ?>
Your code works on my local machine. There is no reason you can't include a .html file using php includes. Try adding error reporting, so php will tell you whats wrong, if it's failing (be sure to disable this on a production machine, you dont want your users seeing the debugging messages).

I suspect a directory problem. Are header.html, footer.html, and index.php all in the same directory?

Re: Including an html file

Posted: Fri Aug 12, 2011 11:03 pm
by The_Mexican
They're all definitely in the same directory. I'm not getting any errors with the error reporting.

The include statements are highlighted pink in the source file, if that matters. I'm thinking maybe I'm missing some crucial part of a php document that they need in order to work, or something? Keep in mind I'm a beginner.

Re: Including an html file

Posted: Fri Aug 12, 2011 11:22 pm
by twinedev
Are you just opening files on your local machine, or from an actual web server? .PHP must be run from a server unlike plain .html files.

When looking at it in FireFox, hit CTRL-U to see the source code, what are you seeing where you are expecting the header and footer. If you are actually seeing <?php include.... ?> then you are either viewing a local copy (not running on a server) or the server you are running it on is not actually executing the PHP files.

Also, when you say it is coming up highlighted pink, what program are you viewing it in to see that?

Re: Including an html file

Posted: Fri Aug 12, 2011 11:25 pm
by califdon
The_Mexican wrote:They're all definitely in the same directory. I'm not getting any errors with the error reporting.
What ARE you getting? You see, we're not sitting next to you, we have no way of knowing what is or isn't happening unless you tell us. Nothing at all? Some text? What?
The_Mexican wrote:The include statements are highlighted pink in the source file, if that matters. I'm thinking maybe I'm missing some crucial part of a php document that they need in order to work, or something? Keep in mind I'm a beginner.
We can help you, but you have to tell us exactly what happens and what should happen but doesn't. As a beginner, that's maybe the most important thing you can learn, so others can understand what you are doing and offer advice.

Re: Including an html file

Posted: Sun Aug 14, 2011 7:09 pm
by The_Mexican
twinedev wrote: If you are actually seeing <?php include.... ?> then you are either viewing a local copy (not running on a server) or the server you are running it on is not actually executing the PHP files.
Well, this must be the problem. I'm viewing a local copy, and not on a server. I was using the source code file on the firefox page to see the highlighted statements.

So is there no way to make this work without a server? If not then I guess I'll just combine everything into one html document...

Thanks for your help.

Re: Including an html file

Posted: Sun Aug 14, 2011 8:01 pm
by califdon
That's right. PHP is a server language. No server, no PHP. Browsers cannot understand PHP. PHP code should never reach a browser, it is used by the server in order to generate HTML, CSS and Javascript code to be sent to the browser. The latter are all client (browser) languages.

Re: Including an html file

Posted: Tue Aug 16, 2011 4:08 am
by phphelpme
You could always try installing:

http://www.apachefriends.org/en/xampp.html

This will work on windows and linux based systems and can provide a great way to setup your own private localhost (server) with ease.

Just install and copy your files into the htdocs folder of the installation directory then start the program control panel, click start apache and php/sql etc and then direct your browser to localhost/ and your cooking with gas.

This will allow you to use php coding on your local machine.

Best wishes,

Shaun.