PHP 脚本在 HTML 文件中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2198680/
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 script not working in HTML file
提问by Anthony
I'm new to PHP. I installed XAMPP and have Apache running. I created helloworld.php in XAMPP's htdocs and got PHP to display in my browser. My question is, why does my PHP script in my HTML file not display in my browser? Ive never installed PHP on its own. Should I also install it? Would it conflict with XAMPP. My code is below. Any assistance will be appreciated. Thanks in advance:
我是 PHP 新手。我安装了 XAMPP 并运行了 Apache。我在 XAMPP 的 htdocs 中创建了 helloworld.php 并让 PHP 显示在我的浏览器中。我的问题是,为什么我的 HTML 文件中的 PHP 脚本没有显示在我的浏览器中?我从来没有自己安装过 PHP。我也应该安装它吗?它会不会与 XAMPP 冲突。我的代码如下。任何帮助将不胜感激。提前致谢:
<html>
<body>
<?php
echo "Hello PHP World";
?>
</body>
</html>
回答by Artjom Kurapov
I assume you are trying to use php inside .html file? Try adding .htaccess file or changing apache config with the following line:
我假设您正在尝试在 .html 文件中使用 php?尝试使用以下行添加 .htaccess 文件或更改 apache 配置:
AddHandler application/x-httpd-php .html
回答by Ignacio Vazquez-Abrams
XAMPP already includes PHP, but unless you end the script name with .phpit is unlikely to be processed by the PHP engine.
XAMPP 已经包含 PHP,但除非您以脚本名称结尾,否则.phpPHP 引擎不太可能处理它。
回答by user2301506
Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...
停止 apache 服务,然后在 c:\xampp\apache\conf\httpd.conf 部分添加一个更改,通过添加...
AddType application/x-httpd-php .html .htm
Restart apache!
重启阿帕奇!
This looks like a big fat 'feature' in the current xampp distribution for win 32-bit.
这看起来像是当前用于 win 32 位的 xampp 发行版中的一个很大的“功能”。
回答by Agus
You should add mime type at http conf for instance in apache at httpd.conf entry
您应该在 http conf 中添加 mime 类型,例如在 apache 中的 httpd.conf 条目
<IfModule mime_module>
#
# TypesConfig points to the file containing the list of mappings from
# filename extension to MIME-type.
#
TypesConfig "conf/mime.types"
.......
AddType application/x-httpd-php .html .htm
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
回答by VolkerK
The php module for apache registers itself as handler for the mime type application/x-httpd-php. And the configuration file apache\conf\extra\httpd-xampp.conf contains the lines
apache 的 php 模块将自己注册为 mime 类型的处理程序application/x-httpd-php。并且配置文件 apache\conf\extra\httpd-xampp.conf 包含以下行
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
which tells the apache that all files having .php as name extension are to be processes by the handler for application/x-httpd-php.
If you (really) want to have your .html files handled by the php module as well you have to add something similar for .html extensions. (there are other methods to tell the apache which extension maps to which mime type/handler. But FilesMatch/SetHandleris fine.)
If you want to enable this "feature" for only one directory you can use an .htaccess fileto change the configuration for that directory (and its subdirectories).
它告诉 apache 所有以 .php 为扩展名的文件都将由application/x-httpd-php的处理程序处理。
如果你(真的)想让你的 .html 文件也由 php 模块处理,你必须为 .html 扩展添加类似的东西。(还有其他方法可以告诉 apache 哪个扩展映射到哪个 mime 类型/处理程序。但是FilesMatch/ SetHandler很好。)
如果您只想为一个目录启用此“功能”,您可以使用.htaccess 文件来更改该目录(及其子目录)的配置。
回答by user7551673
Too much overkill. All these suggestions lead me down the wrong path for like 5 hours. JK, but I did read a lot of google search items all giving wrong answers and each suggestion was just adding more wrong answers.
太过分了。所有这些建议使我走上了错误的道路,大约 5 个小时。JK,但我确实阅读了很多谷歌搜索项,所有这些都给出了错误的答案,每个建议只是添加了更多错误的答案。
The answer is in fact so simple you would want to bang your head: Simply change the file extension from ".html" to ".php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.
答案其实很简单,你可能会想:只需将文件扩展名从“.html”更改为“.php”!!!请记住,您可以完全使用 PHP 构建网页,并且所有 JavaScript 以及基于 JavaScript 构建的内容(例如 JQuery、bootstrap 等)都可以使用。
Here is a simple example of proof:
下面是一个简单的证明示例:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Blank Web Page</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<body>
<?php
$son = 5;
$nos =10;
echo $son + $nos;
?>
<h4>test to see if this html element can be output too!</h4>
<script type="text/javascript" src="js/js.js"></script>
</body>
Notice that I am using your standard html, even though it doesn't show my HTML tags(trust me it's there), web page stuff and have php code inserted inside. Of course the result is 15 and the html element h4 renders correctly too. Change the extension back to "html" and you will get only the h4 element and you will find that your php code has been commented out using multi-comment for html.
请注意,我正在使用您的标准 html,即使它没有显示我的 HTML 标签(相信我它就在那里)、网页内容并在其中插入了 php 代码。当然结果是 15 并且 html 元素 h4 也正确呈现。将扩展名改回“html”,您将只获得 h4 元素,您会发现您的 php 代码已使用 html 的多注释注释掉。
I forgot to add that this works for Xampp too.
我忘了补充一点,这也适用于 Xampp。

