如何将PHP代码/文件添加到HTML(.html)文件?

时间:2012-07-03 13:45:22

标签: php html file

我无法在HTML页面中使用PHP。例如,index.html。我尝试过使用它们:

<? contents ?> 

<?php contents ?> 

这些都不奏效。我的服务器提供PHP,当我使用.php扩展名时,它可以正常工作。这是一个问题还是我必须更改php.ini中的首选项?

12 个答案:

答案 0 :(得分:131)

您无法在.html文件中运行PHP,因为服务器无法将其识别为有效的PHP扩展名,除非您告诉它。为此,您需要在根网站目录中创建.htaccess文件并将此行添加到其中:

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

这将告诉Apache将.htm或.html文件扩展名的文件作为PHP文件进行处理。

答案 1 :(得分:19)

我认为将PHP编写成.html文件会让人感到困惑和反自然。你为什么要这样做?

无论如何,如果你想要的是执行PHP文件并在地址栏中将它们显示为.html,最简单的解决方案就是正常使用.php,并在你的.htaccess中写一条规则:

RewriteRule ^([^.]+)\.html$ $1.php [L]

答案 2 :(得分:14)

为了在.html文件中使用php,您必须将它们与HTTP服务器配置文件中的PHP处理器相关联。在Apache中,看起来像这样:

AddHandler application/x-httpd-php .html

答案 3 :(得分:12)

您可以像其他人一样修改.htaccess,但最快的解决方法是将文件扩展名重命名为.php

答案 4 :(得分:9)

添加此行

AddHandler application/x-httpd-php .html

httpd.conf文件,了解您要执行的操作。 但请记住,如果你这样做,那么你的web服务器将非常慢,因为它将解析甚至不包含php代码的静态代码。 因此,更好的方法是制作文件扩展名.phtml,而不仅仅是.html

答案 5 :(得分:7)

为了解析.html个文件,您需要在服务器配置中设置适当的处理程序。

对于Apache httpd 2.X,这是以下行

AddHandler application/x-httpd-php .html

有关特定服务器安装的信息,请参阅PHP docu

答案 6 :(得分:6)

默认情况下,您不能在HTML页面中使用PHP。

为此,请使用以下内容修改.htacccess文件:

AddType application/x-httpd-php .html

答案 7 :(得分:3)

如果你只在一个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>

这将使PHP可执行文件仅在“yourpage.html”文件中,而不是在所有html页面上,这将防止整个服务器的速度减慢。

至于为什么某人可能想通过html文件提供php,我使用谷歌电子表格中的IMPORTHTML函数从外部URL导入JSON数据,必须用php解析它以清理它并构建一个html表。到目前为止,我还没有找到任何方法将.php文件导入谷歌电子表格,因此必须将其保存为.html文件才能使该功能正常工作。能够通过.html文件提供php是特定用途所必需的。

答案 8 :(得分:2)

使用记事本创建一个空文件,并将其命名为.htaccess,然后将该文件复制到您的项目目录中,并添加此行并保存。

private string CreateSiteCollection(string hostWebUrl, string url, string template, string title, string adminAccount)
{
    // Resolve root site collection URL from host web. We assume that this has been set as the "TenantAdminSite"
    string rootSiteUrl = hostWebUrl.Substring(0, 8 + hostWebUrl.Substring(8).IndexOf("/"));

    //Resolve URL for the new site collection
    var webUrl = string.Format("{0}/sites/{1}", rootSiteUrl, url);

    // Notice that this assumes that AdministrationSiteType as been set as TenantAdministration for root site collection
    // If this tenant admin URI is pointing to site collection whihc is host named site collection, code does create host named site collection as well
    var tenantAdminUri = new Uri(rootSiteUrl);
    string realm = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
    var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, tenantAdminUri.Authority, realm).AccessToken;
    using (var adminContext = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
    {
        var tenant = new Tenant(adminContext);
        var properties = new SiteCreationProperties()
        {
            Url = webUrl,
            Owner = adminAccount,
            Title = title,
            Template = template
        };

        //start the SPO operation to create the site
        SpoOperation op = tenant.CreateSite(properties);
        adminContext.Load(op, i => i.IsComplete);
        adminContext.ExecuteQuery();
    }
    return webUrl;
}

否则使用.php保存.html文件,因为php可以支持html,并将其保存在computer / var / www / html path(linux)

答案 9 :(得分:0)

AJAX也有可能。实际上,您需要php页面中的数据。有了数据后,您就可以在javascript中以任何格式进行格式化并显示。

var xmlhttp = new XMLHttpRequest();
var url = "risingStars.php";

xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        getDbData(this.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();


function getDbData(response) {

//do whatever you want with respone
}

答案 10 :(得分:0)

现代版本:您现在还需要通过更改php-fpm中鲜为人知的“ security.limit_extensions”来修复它。您可能已经知道将apache更改为AddHandler / AddType的详细文献资料机制,在此不再赘述。

  1. 您必须找出php-fpm / conf在您的设置中的位置。我是这样做的

    # grep -rnw '/etc/' -e 'security.limit_extensions'

  2. 我回来了

    '/etc/php-fpm.d/www.conf:387:;security.limit_extensions = .php .php3 .php4 .php5 .php7'

  3. 转到此文件,对其进行编辑,并确保已注释掉它 EG:将其从';security.limit_extensions = .php .php3 .php4 .php5 .php7'更改为<-NB:请注意";"-默认情况下此行实际上是禁用的-而且您不需要所有无用的扩展名,实际上它们可能很危险。到'security.limit_extensions = .php .htm' <-请注意,分号现在已删除。然后重新启动apache /(或nginx)并重新启动php-fpm # service php-fpm restart # service httpd restart

答案 11 :(得分:-3)

对于HTML和PHP的组合,您可以使用.phtml文件。