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 .php
extension, it works properly. Is this a problem or do I have to change the preferences in php.ini
?
这两个工作。我的服务器提供PHP,当我使用. PHP扩展时,它可以正常工作。这是一个问题还是我必须改变ph .ini的偏好?
9 个解决方案
#1
122
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文件来处理文件。
#2
19
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$ $1.php [L]
#3
13
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
#4
11
You can modify .htaccess like others said, but the fastest solution is to rename the file extension to .php
您可以像其他人所说的那样修改.htaccess,但是最快的解决方案是将文件扩展名重命名为.php
#5
8
Add this line
添加这一行
AddHandler application/x-httpd-php .html
to httpd.conf
file 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 .phtml
instead of just .html
.
httpd。conf文件用于您想要做的事情。但是请记住,如果您这样做,那么您的web服务器将非常慢,因为它将解析甚至不包含php代码的静态代码。因此,更好的方法是将文件扩展名.phtml替换为.html。
#6
7
For having .html
files 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。这是下面一行
AddHandler application/x-httpd-php .html
See the PHP docu for information on your specific server installation.
有关特定服务器安装的信息,请参阅PHP文档。
#7
6
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
#8
3
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,我使用谷歌电子表格中的IMPORTHTML函数从一个必须用php解析的外部url导入JSON数据,以清理它并构建一个html表。到目前为止,我还没有找到任何方法将.php文件导入到谷歌电子表格中,因此必须将其保存为.html文件,以便函数工作。能够通过.html文件为php提供服务是该特定用途的必要条件。
#9
-3
For combining HTML and PHP you can use .phtml files.
要组合HTML和PHP,可以使用.phtml文件。
#1
122
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文件来处理文件。
#2
19
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$ $1.php [L]
#3
13
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
#4
11
You can modify .htaccess like others said, but the fastest solution is to rename the file extension to .php
您可以像其他人所说的那样修改.htaccess,但是最快的解决方案是将文件扩展名重命名为.php
#5
8
Add this line
添加这一行
AddHandler application/x-httpd-php .html
to httpd.conf
file 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 .phtml
instead of just .html
.
httpd。conf文件用于您想要做的事情。但是请记住,如果您这样做,那么您的web服务器将非常慢,因为它将解析甚至不包含php代码的静态代码。因此,更好的方法是将文件扩展名.phtml替换为.html。
#6
7
For having .html
files 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。这是下面一行
AddHandler application/x-httpd-php .html
See the PHP docu for information on your specific server installation.
有关特定服务器安装的信息,请参阅PHP文档。
#7
6
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
#8
3
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,我使用谷歌电子表格中的IMPORTHTML函数从一个必须用php解析的外部url导入JSON数据,以清理它并构建一个html表。到目前为止,我还没有找到任何方法将.php文件导入到谷歌电子表格中,因此必须将其保存为.html文件,以便函数工作。能够通过.html文件为php提供服务是该特定用途的必要条件。
#9
-3
For combining HTML and PHP you can use .phtml files.
要组合HTML和PHP,可以使用.phtml文件。