在 HTML 中包含 php 脚本
我是一个新手,以前从未使用过 PHP。我想在 Linux 上从 HTML 文件执行 PHP 脚本。我需要做什么?
这是我的 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>
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?
This is the content of my HTML file:
<!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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您收到什么输出?它只显示 PHP 代码吗?
我想情况就是这样。
对于测试,请将文件的扩展名更改为
.php
并再次运行。现在有用吗?如果是这样,您需要将 PHP 与服务器上的
.html
和.htm
文件相关联。编辑
这是您要添加到 Apache 配置中的行:
What output do you receive? Does it just show the PHP code?
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?If so, you will need to associate PHP with
.html
and.htm
files on your server.EDIT
This is the line you want to add to your Apache config:
您可以使用
标记将 php 代码直接嵌入到 HTML 中,并将 php 代码放在开始和结束标记之间。
在此之前,您需要在 /etc/apache2/apache2.conf 文件中添加此处理程序以在 .html 文件中运行 php 脚本:
AddType application/x-httpd-php .html
You can embed the php code right into HTML using
<?php ?>
tag and putting your php code between opening and closing tags.Before that you need to add this handler in /etc/apache2/apache2.conf file to run php scripts inside .html file:
AddType application/x-httpd-php .html
您可以运行将文件路径作为参数传递给
php
命令行程序,或者配置 Web 服务器以将页面作为 PHP 执行(具体取决于您使用的 Web 服务器)访问页面的 URL。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.这是在 html 文件中包含 php 代码的分步过程(已测试)
如果 PHP 正常工作,则只需在带有 *.html 或 *.htm 扩展名的文件中使用 PHP 脚本即可出色地。神奇的词是“.htaccess”。请参阅 .htaccess 的维基百科定义以了解更多信息。根据维基百科,它是“一个目录级配置文件,允许对 Web 服务器配置进行分散管理”。
您或许可以使用这样的 .htaccess 配置文件来达到您的目的。在我们的例子中,您希望网络服务器像 PHP 文件一样解析 HTML 文件。
首先,创建一个空白文本文件并将其命名为“.htaccess”。您可能会问自己为什么文件名以点开头。在类 Unix 系统上,这意味着它是一个点文件,是一个隐藏文件。
(注意:如果您的操作系统不允许文件名以点开头,只需暂时将文件命名为“xyz.htaccess”。在后续步骤中将其上传到网络服务器后,您可以在线将该文件重命名为“.htaccess”。 htaccess”)
接下来,使用简单的文本编辑器(例如 MS Windows 中的“编辑器”)打开该文件。将以下行粘贴到文件中:
AddType 应用程序/x-httpd-php .html .htm
如果这不起作用,请从文件中删除上面的行并将此替代行粘贴到其中(对于 PHP5):
AddType 应用程序/x-httpd-php5 .html .htm
现在将 .htaccess 文件上传到网络服务器的根目录。确保文件名是“.htaccess”。您的网络服务器现在应该像 PHP 文件一样解析 *.htm 和 *.html 文件。
您可以通过创建如下所示的 HTML 文件来尝试它是否有效。将其命名为“php-in-html-test.htm”,将以下代码粘贴到其中并将其上传到网络服务器的根目录:
尝试通过输入以下内容在浏览器中打开该文件: http://www.your-domain.com/php-in-html-test.htm (再次,请将 your-domain.com 替换为你自己的域名...)
如果您的浏览器显示短语“It Works!”一切正常,从现在开始您可以在 .*html 和 *.htm 文件中使用 PHP。但是,如果没有,请尝试使用 .htaccess 文件中的替代行,如上面所示。如果仍然不起作用,请联系您的托管提供商。
Here is the step by step process to include php code in html file ( Tested )
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."
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.
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.
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:
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.
我很惊讶没有人提到一种破解它的方法 - 如果你不想麻烦地更改 Apache 配置,你可以这样做:
在你的 .html 文件中,你只需使用 iframes
这将只是显示所显示的内容,就好像您直接访问 www.mysite.com/myScript.php
因此,例如我们可以
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:
In your .html file you simply make use out of iframes
This will simply just display what ever is displayed as if you went directly to www.mysite.com/myScript.php
So for example we could have