简体   繁体   English

如何在 HTML 文件中运行 PHP 脚本?

[英]How can I run a PHP script inside a HTML file?

如何在.html 文件中运行简单的 PHP 代码?

To execute 'php' code inside 'html' or 'htm', for 'apache version 2.4.23'在 'html' 或 'htm' 中执行 'php' 代码,用于 'apache version 2.4.23'

Go to '/etc/apache2/mods-enabled' edit '@mime.conf'转到“/etc/apache2/mods-enabled”编辑“@mime.conf”

Go to end of file and add the following line:转到文件末尾并添加以下行:

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

BEFORE tag '< /ifModules >' verified and tested with 'apache 2.4.23' and 'php 5.6.17-1' under 'debian' BEFORE 标记 '< /ifModules >' 在 'debian' 下用 'apache 2.4.23' 和 'php 5.6.17-1' 验证和测试

You can't run PHP in an html page ending with .html .不能在.html结尾的 html 页面中运行PHP Unless the page is actually PHP and the extension was changed with .htaccess from .php to .html除非页面实际上是 PHP 并且扩展名已使用.htaccess.php更改为.html

What you mean is:你的意思是:

index.html
<html>
...
<?php echo "Hello world";?> //This is impossible


index.php //The file extension can be changed using htaccess, ex: its type stays php but will be visible to visitors as index.html

<?php echo "Hello world";?>

thanks for the ideas but none works here.感谢您的想法,但在这里没有任何工作。 So i did that... I am using xampp last version on 2014. go to \\xampp\\apache\\conf\\extra\\httpd-xampp.conf .所以我这样做了......我在 2014 年使用 xampp 最后一个版本。去\\xampp\\apache\\conf\\extra\\httpd-xampp.conf

we will find this bit of code:我们会找到这段代码:

<IfModule php5_module>
    **<FilesMatch "\.php$">**
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>

    PHPINIDir "C:/xampp/php"
</IfModule>

Focus on second line, so we must to change to:重点放在第二行,所以我们必须改成:

<IfModule php5_module>
    **<FilesMatch "\.(php|html)$">**
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>

    PHPINIDir "C:/xampp/php"
</IfModule>

And that is it.就是这样。 Works good!效果很好!

Simply you cant !!简直不能!! but you have some possbile options :但你有一些可能的选择:

1- Excute php page as external page. 1- 将 php 页面作为外部页面执行。

2- write your html code inside the php page itself. 2- 将您的 html 代码写入 php 页面本身。

3- use iframe to include the php within the html page. 3- 使用 iframe 在 html 页面中包含 php。

to be more specific , unless you wanna edit your htaccess file , you may then consider this:更具体地说,除非你想编辑你的 htaccess 文件,否则你可以考虑这个:

http://php.about.com/od/advancedphp/p/html_php.htm http://php.about.com/od/advancedphp/p/html_php.htm

You need to make the extension as .php to run a php code BUT if you can't change the extension you could use Ajax to run the php externally and get the result您需要将扩展​​名设为 .php 才能运行 php 代码,但是如果您无法更改扩展名,则可以使用 Ajax 在外部运行 php 并获得结果

For eg:例如:

<html>
<head>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $.ajax({
        url:'php_File_with_php_code.php',
        type:'GET', 
        data:"parameter=some_parameter",
       success:function(data)
       {
              $("#thisdiv").html(data);
           }
    });
});
</script>
</head>
<body>
<div id="thisdiv"></div>
</body>
</html>

Here, the JQuery is loaded and as soon as the pages load, the ajax call a php file from where the data is taken, the data is then put in the div在这里,JQuery 被加载,一旦页面加载,ajax 就会调用一个从中获取数据的 php 文件,然后将数据放入 div

Hope This Helps希望这可以帮助

Yes, you can run PHP in an HTML page.是的,您可以在 HTML 页面中运行 PHP。

I have successfully executed PHP code in my HTML files for many years.多年来,我已经成功地在我的 HTML 文件中执行了 PHP 代码。 (For the curious, this is because I have over 8,000 static HTML files created by me and others over the last 20 years and I didn't want to lose search engine ranking by changing them and, more importantly, I have too many other things to work on). (出于好奇,这是因为我和其他人在过去 20 年中创建了 8,000 多个静态 HTML 文件,我不想通过更改它们而失去搜索引擎排名,更重要的是,我还有太多其他东西继续工作)。

I am not an expert -- below is what I've tried and what works for me.我不是专家 - 以下是我尝试过的以及对我有用的方法。 Please don't ask me to explain it.请不要让我解释它。

Everything below involves adding a line or two to your .htaccess file.下面的所有内容都涉及在 .htaccess 文件中添加一两行。

Here is what one host ( http://simolyhosting.net ) support did for me in 2008 -- but it no longer works for me now.以下是 2008 年一位主机 ( http://simolyhosting.net ) 支持为我所做的事情——但现在它不再适用于我。

AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php5 .htm .html

That solution appears to be deprecated now, though it might work for you.该解决方案现在似乎已被弃用,尽管它可能对您有用。

Here's what's working for me now:这是现在对我有用的方法:

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

(This page has PHP code that executes properly with the above solution -- http://mykindred.com/bumstead/steeplehistory.htm ) (此页面具有使用上述解决方案正确执行的 PHP 代码 - http://mykindred.com/bumstead/steeplehistory.htm


Below are other solutions I found -- they are NOT MINE:以下是我找到的其他解决方案——它们不是我的:


https://forums.cpanel.net/threads/cant-execute-php-in-html-since-ea4-upgrade.569531 https://forums.cpanel.net/threads/cant-execute-php-in-html-since-ea4-upgrade.569531

I'm seeing this across many servers I've recently upgraded to EA4.我在最近升级到 EA4 的许多服务器上都看到了这一点。 Using cPanel Apache handlers or adding this directly in to .htaccess (same as cPanel does through gui add handlers):使用 cPanel Apache 处理程序或将其直接添加到 .htaccess(与 cPanel 通过 gui 添加处理程序所做的相同):

AddHandler application/x-httpd-php5 .html

Sep 9, 2016 2016 年 9 月 9 日

AddHandler application/x-httpd-ea-php56 .html

https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/parsing-php-code-within-html-pages-a602364.html https://help.1and1.com/hosting-c37630/scripts-and-programming-languages-c85099/php-c37728/parsing-php-code-within-html-pages-a602364.html

Open a text editor such as wordpad, notepad, nano, etc. and add the following line:打开写字板、记事本、nano 等文本编辑器,添加以下行:

AddHandler x-mapp-php5 .html .htm

If you want to use PHP 5.4 instead of PHP 5.2 then use the following line instead:如果您想使用 PHP 5.4 而不是 PHP 5.2,请使用以下行:

AddHandler x-mapp-php6 .html .htm

https://www.godaddy.com/community/Developer-Cloud-Portal/Running-php-in-html-files/td-p/2776 https://www.godaddy.com/community/Developer-Cloud-Portal/Running-php-in-html-files/td-p/2776

To run HTML using FastCGI/PHP, try adding this code to the .htaccess file for the directory the script is in:要使用 FastCGI/PHP 运行 HTML,请尝试将此代码添加到脚本所在目录的 .htaccess 文件中:

Options +ExecCGI
AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html

You can add additional lines for other file extensions if needed.如果需要,您可以为其他文件扩展名添加额外的行。

<?php 
     echo '<p>Hello World</p>' 
?>

As simple as placing something along those lines within your HTML assuming your server is set-up to execute PHP in files with the HTML extension.假设您的服务器设置为在具有 HTML 扩展名的文件中执行 PHP,就像在 HTML 中沿着这些行放置一些东西一样简单。

I'm not sure if this is what you wanted, but this is a very hackish way to include php.我不确定这是否是您想要的,但这是包含 php 的一种非常hackish 的方式。 What you do is you put the php you want to run in another file, and then you include that file in an image.您所做的是将要运行的 php 放在另一个文件中,然后将该文件包含在图像中。 For example:例如:

RunFromHTML.php运行从HTML.php

<?php
  $file = fopen("file.txt", "w");
  //This will create a file called file.txt,
  //provided that it has write access to your filesystem
  fwrite($file, "Hello World!");
  //This will write "Hello World!" into file.txt
  fclose($file);
  //Always remember to close your files!
?>

RunPhp.html运行php.html

<html>
  <!--head should be here, but isn't for demonstration's sake-->
  <body>
    <img style="display: none;" src="RunFromHTML.php">
    <!--This will run RunFromHTML.php-->
  </body>
</html>

Now, after visiting RunPhp.html, you should find a file called file.txt in the same directory that you created the above two files, and the file should contain "Hello World!"现在,访问 RunPhp.html 后,您应该在创建上述两个文件的同一目录中找到一个名为 file.txt 的文件,该文件应包含“Hello World!” inside of it.在它里面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM