用于 HTML 的 PHP 包含?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11887429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Include for HTML?
提问by sresht
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:
我有一个导航栏、一个图像和一个标题,我将包含在我网站的每个页面中,因此我想使用 php include 在多个页面中引用此代码。但是,我认为我可能有语法错误或其他原因,因为加载它时它没有呈现任何内容。下面是一些代码片段:
<!-- 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
<?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!
感谢您的帮助!
回答by Edward Ruchevits
You can use phpcode in files with extension .phpand only there (iff other is not defined in your server settings).
您可以php在带有扩展名的文件中使用代码,.php并且只能在那里使用(如果其他未在您的服务器设置中定义)。
Just rename your file *.htmlto *.php
只需将您的文件重命名*.html为*.php
If you want to allow phpcode processing in files of different format, you have two options to do that:
如果您想允许php在不同格式的文件中处理代码,您有两种选择:
1) Modifying httpd.confto allow this for all projects on your server, by adding:
1)httpd.conf通过添加以下内容进行修改以允许服务器上的所有项目使用此功能:
AddHandler application/x-httpd-php .htm .html
2) Creating .htaccessfile in your separate project top directory with:
2).htaccess在您单独的项目顶级目录中创建文件:
<Files />
AddType application/x-httpd-php .html
</Files>
For second option you need to allow use of .htaccessfiles in your httpd.conf, by adding the following settings:
对于第二个选项,您需要通过添加以下设置来允许使用 中的.htaccess文件httpd.conf:
AllowOverride All
AccessFileName .htaccess
*that is correct for Apache HTTP Server
*这对于 Apache HTTP Server 是正确的
回答by Alexander Wigmore
You don't need to be echoing the info within the php file. A php includewill automatically include any HTML within that file.
您不需要在 php 文件中回显信息。phpinclude将自动包含该文件中的任何 HTML。
Make sure you're actually using a index file with a .phpextension, .htmlwon't work with php includes. (Unless you're telling your server to treat .html files otherwise)
确保您实际上使用的是带有.php扩展名的索引文件,.html不能使用 php 包含。(除非您告诉服务器以其他方式处理 .html 文件)
Make sure your paths are correctly set up. From your description, the way you've set it up your header.php/navbar.php/image.php files should be in your root directory. So your root directory should look like this:
确保您的路径设置正确。根据您的描述,您设置 header.php/navbar.php/image.php 文件的方式应该在您的根目录中。所以你的根目录应该是这样的:
index.php
navbar.php
image.php
header.php
index.php
navbar.php
image.php
header.php
Otherwise if those PHP files are in a folder called /includes/, it should look like so:
否则,如果这些 PHP 文件位于名为 的文件夹中/includes/,则应如下所示:
<?php include ('includes/headings.php'); ?>
<?php include ('includes/headings.php'); ?>
回答by raidenace
Try to get some debugging information, could be that the file path is wrong, for example.
尝试获取一些调试信息,例如可能是文件路径错误。
Try these two things:- Add this line to the top of your sample page:
尝试以下两件事:- 将此行添加到示例页面的顶部:
<?php error_reporting(E_ALL);?>
This will print all errors/warnings/notices in the page so if there is any problem you get a text message describing it instead of a blank page
这将打印页面中的所有错误/警告/通知,因此如果有任何问题,您会收到一条描述它的文本消息而不是空白页
Additionally you can change include() to require()
此外,您可以将 include() 更改为 require()
<?php require ('headings.php'); ?>
<?php require ('navbar.php'); ?>
<?php require ('image.php'); ?>
This will throw a FATAL error PHP is unable to load required pages, and should help you in getting better tracing what is going wrong..
这将抛出一个致命错误 PHP 无法加载所需的页面,并且应该可以帮助您更好地跟踪出了什么问题。
You can post the error descriptions here, if you get any, and you are unable to figure out what it means..
您可以在此处发布错误说明,如果您有任何错误说明,但您无法弄清楚它的含义..
回答by napender
Here is the step by step process to include php code in html file ( Tested )
这是在 html 文件中包含 php 代码的分步过程(已测试)
If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."
如果 PHP 可以正常工作,那么只剩下一步就可以在带有 *.html 或 *.htm 扩展名的文件中使用 PHP 脚本了。神奇的词是“.htaccess”。请参阅 .htaccess 的维基百科定义以了解更多信息。根据维基百科,它是“一个目录级配置文件,允许对 Web 服务器配置进行分散管理”。
You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.
您可能可以将这样的 .htaccess 配置文件用于您的目的。在我们的例子中,您希望网络服务器解析 HTML 文件,如 PHP 文件。
First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file. (Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess") Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file: AddType application/x-httpd-php .html .htm If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5: AddType application/x-httpd-php5 .html .htm Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.
首先,创建一个空白文本文件并将其命名为“.htaccess”。您可能会问自己为什么文件名以点开头。在类 Unix 系统上,这意味着它是一个点文件是一个隐藏文件。(注意:如果您的操作系统不允许以点开头的文件名,请暂时将文件命名为“xyz.htaccess”。一旦您在稍后的步骤中将其上传到您的网络服务器,您就可以在线将文件重命名为“.htaccess”。 htaccess") 接下来,使用简单的文本编辑器(如 MS Windows 中的“编辑器”)打开文件。将以下行粘贴到文件中: AddType application/x-httpd-php .html .htm 如果这不起作用,请从文件中删除上面的行并将此替代行粘贴到其中,对于 PHP5:AddType application/x- httpd-php5 .html .htm 现在将 .htaccess 文件上传到您的网络服务器的根目录。确保文件名是“.htaccess”。您的网络服务器现在应该像 PHP 文件一样解析 *.htm 和 *.html 文件。
You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:
您可以通过创建如下所示的 HTML 文件来尝试它是否有效。将其命名为“php-in-html-test.htm”,将以下代码粘贴到其中并上传到您的网络服务器的根目录:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Use PHP in HTML files</TITLE>
</HEAD>
<BODY>
<h1>
<?php echo "It works!"; ?>
</h1>
</BODY>
</HTML>
Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm(once again, please replace your-domain.com by your own domain...) If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.
尝试通过键入以下内容在浏览器中打开该文件:http: //www.your-domain.com/php-in-html-test.htm(再次,请用您自己的域替换 your-domain.com.. .) 如果您的浏览器显示“It works!” 一切正常,从现在开始,您可以在 .*html 和 *.htm 文件中使用 PHP。但是,如果没有,请尝试使用我们上面显示的 .htaccess 文件中的替代行。如果仍然无法正常工作,请联系您的托管服务提供商。
回答by chitech
I figured out a simple solution to the PHP include stuff. Simply rename all your .html files to .php and your're good to go.
我想出了一个简单的解决方案来解决 PHP 包含的东西。只需将所有 .html 文件重命名为 .php 即可。
回答by Alex S
I have a similar issue. It appears that PHP does not like php code inside included file. In your case solution is quite simple. Remove php code from navbar.php, simply leave plain HTML in it and it will work.
我有一个类似的问题。PHP 似乎不喜欢包含文件中的 php 代码。在您的情况下,解决方案非常简单。从 navbar.php 中删除 php 代码,只需在其中保留纯 HTML 即可。
回答by Rudolph Gottesheim
First: what the others said. Forget the echo statement and just write your navbar.php as a regular HTML file.
第一:别人怎么说。忘记 echo 语句,只需将您的 navbar.php 编写为常规 HTML 文件。
Second: your include paths are probably messed up. To make sure you include files that are in the same directory as the current file, use __DIR__:
第二:你的包含路径可能搞砸了。要确保包含与当前文件位于同一目录中的文件,请使用__DIR__:
include __DIR__.'/navbar.php'; // PHP 5.3 and later
include dirname(__FILE__).'/navbar.php'; // PHP 5.2

