Linux 在 HTML 中包含 php 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6416418/
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
include php script inside HTML
提问by Tu Hoang
I am a newbie and have never used PHP before. I want to execute PHP script from an HTML file on Linux. What do I need to do?
我是新手,以前从未使用过 PHP。我想从 Linux 上的 HTML 文件执行 PHP 脚本。我需要做什么?
This is the content of my HTML file:
这是我的 HTML 文件的内容:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Testing Weather Data</TITLE>
</HEAD>
<BODY>
<div id="weather">
<p>Weather Information
<?php include "/home/tahoang/Desktop/weatherData.php"; ?>
</div>
</BODY>
</HTML>
采纳答案by djdy
What output do you receive? Does it just show the PHP code?
你得到什么输出?它只显示PHP代码吗?
I assume that's the case.
我认为是这种情况。
For a test, change the extension on the file to .php
and run it again. Does it work now?
对于测试,将文件的扩展名更改为.php
并再次运行它。现在有效吗?
If so, you will need to associate PHP with .html
and .htm
files on your server.
如果是这样,你需要PHP与关联.html
和.htm
您的服务器上的文件。
EDIT
编辑
This is the line you want to add to your Apache config:
这是您要添加到 Apache 配置中的行:
AddType application/x-httpd-php .html
回答by Quentin
You either run pass the file path as an argument to the php
command line program, or you configure a web server to execute the page as PHP (the specifics of which depend on the web server you use) then visit the URL of the page.
您要么运行将文件路径作为参数传递给php
命令行程序,要么配置 Web 服务器以将页面作为 PHP 执行(具体取决于您使用的 Web 服务器),然后访问该页面的 URL。
回答by Tarik
You can embed the php code right into HTML using <?php ?>
tag and putting your php code between opening and closing tags.
您可以使用<?php ?>
标签将php 代码直接嵌入到 HTML 中,并将您的 php 代码放在开始和结束标签之间。
Before that you need to add this handler in /etc/apache2/apache2.conffile to run php scripts inside .html file:
在此之前,您需要在/etc/apache2/apache2.conf文件中添加此处理程序以在 .html 文件中运行 php 脚本:
AddType application/x-httpd-php .html
AddType application/x-httpd-php .html
回答by Nicky Thomas
I am surprised no-one has mentioned a way to hack it - if you don't want to hassle of changing the Apache config here is how you do it:
我很惊讶没有人提到一种破解它的方法 - 如果你不想麻烦地更改 Apache 配置,这里是你的方法:
In your .html file you simply make use out of iframes
在您的 .html 文件中,您只需使用 iframe
<iframe name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>
This will simply just display what ever is displayed as if you went directly to www.mysite.com/myScript.php
这将只是显示所显示的内容,就像您直接访问 www.mysite.com/myScript.php 一样
So for example we could have
所以例如我们可以有
<?php
echo 'Hello World!';
?>
回答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 文件中的替代行。如果仍然无法正常工作,请联系您的托管服务提供商。