如何将 PHP 代码/文件添加到 HTML(.html) 文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11312316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:14:35  来源:igfitidea点击:

How do I add PHP code/file to HTML(.html) files?

phphtmlfile

提问by Hoon

I can't use PHP in my HTML pages. For example, index.html. I've tried using both:

我不能在我的 HTML 页面中使用 PHP。例如,index.html。我试过同时使用:

<? contents ?> 

and

<?php contents ?> 

Neither of these work. My server offers PHP, and when I use a the .phpextension, it works properly. Is this a problem or do I have to change the preferences in php.ini?

这些都不起作用。我的服务器提供 PHP,当我使用.php扩展时,它可以正常工作。这是一个问题还是我必须更改 中的首选项php.ini

回答by John Conde

You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

您不能在 .html 文件中运行 PHP,因为除非您告诉它,否则服务器不会将其识别为有效的 PHP 扩展名。为此,您需要在根 Web 目录中创建一个 .htaccess 文件并将以下行添加到其中:

AddType application/x-httpd-php .htm .html

This will tell Apache to process files with a .htm or .html file extension as PHP files.

这将告诉 Apache 将具有 .htm 或 .html 文件扩展名的文件作为 PHP 文件处理。

回答by David Morales

I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??

我认为将 PHP 写入 .html 文件是令人困惑和反自然的。为什么要这么做??

Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:

无论如何,如果您想要执行 PHP 文件并在地址栏中将它们显示为 .html,最简单的解决方案是正常使用 .php,并在您的 .htaccess 中编写如下规则:

RewriteRule ^([^.]+)\.html$ .php [L]

回答by Chris Trahey

In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:

为了在 .html 文件中使用 php,您必须在 HTTP 服务器的配置文件中将它们与您的 PHP 处理器相关联。在 Apache 中,它看起来像这样:

AddHandler application/x-httpd-php .html

回答by CAAY

You can modify .htaccess like others said, but the fastest solution is to rename the file extension to .php

您可以像其他人说的那样修改 .htaccess,但最快的解决方案是将文件扩展名重命名为 .php

回答by Badar

Add this line

添加这一行

AddHandler application/x-httpd-php .html

to httpd.conffile for what you want to do. But remember, if you do this then your web server will be very slow, because it will be parsing even static code which will not contain php code. So the better way will be to make the file extension .phtmlinstead of just .html.

httpd.conf文件为你想要做什么。但是请记住,如果您这样做,那么您的 Web 服务器将非常慢,因为它甚至会解析不包含 php 代码的静态代码。所以更好的方法是制作文件扩展名,.phtml而不仅仅是.html.

回答by Sirko

For having .htmlfiles parsed as well, you need to set the appropriate handler in your server config.

为了同时.html解析文件,您需要在服务器配置中设置适当的处理程序。

For Apache httpd 2.X this is the following line

对于 Apache httpd 2.X,这是以下行

AddHandler application/x-httpd-php .html

See the PHP docufor information on your specific server installation.

有关特定服务器安装的信息,请参阅PHP 文档

回答by Kerry Ritter

By default you can't use PHP in HTML pages.

默认情况下,您不能在 HTML 页面中使用 PHP。

To do that, modify your .htacccess file with the following:

为此,请使用以下内容修改您的 .htacccess 文件:

AddType application/x-httpd-php .html

回答by MistyDawn

If you only have php code in one html file but have multiple other files that only contain html code, you can add the following to your .htaccess file so it will only serve that particular file as php.

如果您在一个 html 文件中只有 php 代码,但有多个仅包含 html 代码的其他文件,您可以将以下内容添加到您的 .htaccess 文件中,这样它只会将该特定文件作为 php 提供。

<Files yourpage.html>
AddType application/x-httpd-php .html 
//you may need to use x-httpd-php5 if you are using php 5 or higher
</Files>

This will make the PHP executable ONLY on the "yourpage.html" file and not on all of your html pages which will prevent slowdown on your entire server.

这将使 PHP 可执行文件仅在“yourpage.html”文件上,而不是在所有 html 页面上,这将防止整个服务器变慢。

As to why someone might want to serve php via an html file, I use the IMPORTHTML function in google spreadsheets to import JSON data from an external url that must be parsed with php to clean it up and build an html table. So far I haven't found any way to import a .php file into google spreadsheets so it must be saved as an .html file for the function to work. Being able to serve php via an .html file is necessary for that particular use.

至于为什么有人可能想要通过 html 文件提供 php,我使用 google 电子表格中的 IMPORTHTML 函数从必须用 php 解析的外部 url 导入 JSON 数据以清理它并构建一个 html 表。到目前为止,我还没有找到将 .php 文件导入谷歌电子表格的任何方法,因此必须将其保存为 .html 文件才能使该功能正常工作。能够通过 .html 文件提供 php 是该特定用途所必需的。

回答by Progga Ilma

Create an empty file using notepad and name it .htaccess ,Then copy that file in your project directory and add this line and save.

使用记事本创建一个空文件并将其命名为 .htaccess ,然后将该文件复制到您的项目目录中并添加这一行并保存。

AddType application/x-httpd-php .htm .html

else save the .html file using .php as php can support html,and save it in computer/var/www/html path(linux)

否则使用.php保存.html文件,因为php可以支持html,并将其保存在computer/var/www/html路径(linux)

回答by Ravi Ganesh

AJAX is also a possibility. Effectively you would want data from the php page. Once you have the data you can format in anyway in javascript and display.

AJAX 也是一种可能性。实际上,您需要来自 php 页面的数据。获得数据后,您可以在 javascript 中以任何方式格式化并显示。

var xmlhttp = new XMLHttpRequest();
var url = "risingStars.php";

xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        getDbData(this.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();


function getDbData(response) {

//do whatever you want with respone
}