tags:

views:

87

answers:

8

Hi there, I'm trying to put the top of my page (which is all html) in a seperate file so I only need to change things once like the menu bar etc. So I googled some and found a solution on this website as well, but it doesn't work and I don't know why.

So I have a page like article.html and on the top I have this:

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01         
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html lang="en">
<head>

<?php  include("/pages/header.php");   ?> 

<!-- rest of the html code -->

In the header.php I have the html that should be on the top of each page and it starts with:

<?php
/**
 * @author name
 * website header
 */
?>

<!-- html code -->

So what's wrong with this. When I open the page article.html and right click on it to view the source, I can see the php from above calling the header.php file.

Thanks!

FYI: I have php enabled on the server

EDIT: I changed the file path to '../pages/header.php' and now it works. I have no clue why it works like this..?

+2  A: 

You put a '/' at the beginning of your relative path. You should remove it. That makes it an absolute path. It should probably be include("pages/header.php");

Fletcher Moore
It shouldn't be relative, because the path should always start from the root folder. the header.php is only present in root/pages/header.php nd other .html pages may not be in the root
Andy
Your server is going to go hunting for that file, not the browser. Your server is going to assume the root means the root of the computer it is on, not the root of the domain.
Fletcher Moore
@Andy: But then, you still miss the `/root/` part. If you use absolute paths in PHP's include, they are evaluated *really* absolute, that is, from your OS's root dir (chroot aside; like `/var/www/demo/root/pages/header.php`).
Boldewyn
@Andy you messed up a *filesystem root* with a web-server root.
Col. Shrapnel
What I meant was the web-server root obviously. Say I have a file in webroot/common/page.html that requires the header file in webroot/pages/header.php then it needs to start from the webroot with looking right...
Andy
+4  A: 

You can't use php code in a html file.

Rename article.html to article.php .

klez
You answered 43 seconds before I did :D
Cristian
Isn't it possible to embed php in html at all?
Andy
@Casidiablo, yeah, usually people answer 43 seconds before I do,so a bit of change is welcomed :D
klez
@Andy: you'd better rename the file. Nothing else is needed so I don't think it would be so much a problem.
klez
@Andy: ...or you configure your Apache/lighty/IIS/whatever server to parse .html files through PHP. However, with a view to performance, this is generally not suggested.
Boldewyn
+2  A: 

I think the page where you have this: <?php include("/pages/header.php"); ?> is not a PHP file... have you checked its file extension? It must be: article.php

Cristian
it is a .html file, not a php one. But embedding php code into html should be possible.
Andy
@Andy, not if your server sends HTML out without sending it through a PHP parser first.
richsage
+1  A: 

It sounds as if your server is set up to not parse .html files as PHP files. This is the normal setup - try renaming your article.html to article.php and see if that solves the problem. If it does, you can either reconfigure your server to parse .html files as PHP (ask on http://serverfault.com/ for details), or use mod_rewrite to redirect .html files to their .php equivalent

richsage
Yes, I just tried renaming it to .php and it worked.I'm using my home server, I will try later on the server of my hosting provider to see if they parse .html as PHP.I knew embedding php had to be possible to html!
Andy
@Andy, most servers won't be set up to do that as it incurs extra overhead when for the majority of cases it's not needed (think sites that don't use PHP and are just standard HTML pages).
richsage
+2  A: 

include, require, include_once and require_once works best with full server paths. Try to use something like:

<?php include($_SERVER['DOCUMENT_ROOT'] . '/pages/header.php'); ?>

From the PHP docs:

Files are included based on the file path given or, if none is given, the include_path specified. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.

If a path is defined (full or relative), the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

For more information on how PHP handles including files and the include path, see the documentation for include_path.

Note that the relative starting point is always from the file being executed. Thus using relative and absolute paths in include can lead to errors if you include files from many directories and if the entry point can be executed from many directories (damn, it's hard to explain :)).

Also, as others pointed out, you can't use PHP statements in a plain HTML file (at least by default). Those statements will be treated as plain text. Either rename your html file to php, or make an .htaccess to tell the server to treat this file as a php file.

AlexV
The only good answer. though I'd say there is no matter relative or absolute path, as long as path is correct, while /pages/header.php is incorrect one for sure
Col. Shrapnel
It's weird, but this doesn't work at all, maybe some typo somewhere?
Andy
When you go in your hosting space with your FTP software, what is the FULL path shown when you are in the "pages" directory?
AlexV
/mydomain/pages as far as I can see. I use FileZilla
Andy
And when you <?php echo $_SERVER['DOCUMENT_ROOT']; ?> what do you get?
AlexV
the browser shows this:/ --> and then tries to render the page as good as it can without all the info from the header file
Andy
BTW, this is the error the browser shows when using the code in the original answer, but without the '/' before pages:Warning: main() [function.include]: Failed opening '/pages/header.php' for inclusion (include_path='.:/usr/local/php4/share/pear') in /home/www/mydomain.com/common/article.html on line 5
Andy
I found what was wrong...the file path should be '../pages/header.php'yes, with the .. I have no clue why it works that way
Andy
Try to include: '/home/www/mydomain.com/pages/header.php'. As you can see, now he's trying to find '/pages/header.php' in '.:/usr/local/php4/share/pear'...
AlexV
@AlexV: yes, that works as well. I don't know what's better to use? that what you are proposing or the ../ solution I found
Andy
IMO $_SERVER['DOCUMENT_ROOT'] should contain '/home/www/mydomain.com' and my original solution should work. But I've seen some webservers badly configured in the past. Check with your host if it's normal meanwhile you can use either solution since your use of PHP is simple (for more "complex" use of include/require - see my post - a full server path is always prefered).
AlexV
A: 

I didn't read your question fully. It actually sounds like you aren't passing your code through a PHP interpreter. PHP files are code that must be interpreted by a program on your server. They tend to end in the extension .php You need to see if your host offers PHP. They probably do. If so, you might try just renaming the file to article.php.

Fletcher Moore
that's probably it, will have to check my home server settings and will try it out on my hosting server
Andy
@Andy what settings you gonna check and what will you try? To rename a file? It isn't *that* hard :)
Col. Shrapnel
settings for parsing php as html...renaming works fine, but will have to rename every single other link in all pages
Andy
A: 

Check your php configuration file (on my server it's /etc/apache/mod_php.conf) for the following lines

# Load the PHP module
LoadModule php5_module libexec/apache/libphp5.so

# Tell Apache to feed all *.php files through the PHP module
AddType application/x-httpd-php .php .phtml

Then, make sure every single file either include'd or requested directly has the extension .php.

amphetamachine
A: 

I think you get a good grasp how including works from all the other answers, but to complicate things a bit, I'd like to mention, that you could go the other way round: Include .html files within .php files works as you expect (that is, when you set the path with regard to the server, not the docroot).

Why this? You learn from the other answers, that the server, when a PHP file is requested, puts it, throws it through the php program and outputs the result to the user. Therefore you must rename the original file to .php (so that the server does this).

Now, inside PHP, you have the include statement. As you learned, it fetches another file and embeds it at its position just like if the content would be there literally (that is, supposing an additional ?> before and a <?php after).

If you include a simple HTML file now, PHP fetches the file and puts it in place, evaluating all code in it. Since HTML files are valid PHP files, too, all that happens, is, that the HTML is sent to the browser.

To put it in a nutshell: Try to understand, what tool does what:

  • Browser (or, more generally, client) sends a request
  • Server (e.g., Apache) tries to find a file that suits the request
  • Server, too, sees: "Oh, a PHP file. I should pipe it through the PHP rpogram, says my config"
  • PHP looks inside the stuff from the server through every <?php ... ?> and tries to evaluate that as PHP code. Everything else is left alone and sent to the browser as is.
  • Browser gets the parsed response, that is, without any <?php ... ?> segments in it, because they were handled and then removed by PHP prior to sending them

In your case: The include statement is within a PHP code segment, and is therefore a part of what PHP 'sees'. It will be evaluated on the server and never sent to the browser.

Boldewyn