开发者

embedded php in html to include header (top part of page) into the page

开发者 https://www.devze.com 2022-12-29 06:35 出处:网络
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.

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">
<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 开发者_JAVA百科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..?


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

Rename article.html to article.php .


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");


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


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.


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 https://serverfault.com/ for details), or use mod_rewrite to redirect .html files to their .php equivalent


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消