[php] How do I add PHP code/file to HTML(.html) files?

I can't use PHP in my HTML pages. For example, index.html. I've tried using both:

<? contents ?> 

and

<?php contents ?> 

Neither of these work. My server offers PHP, and when I use a the .php extension, it works properly. Is this a problem or do I have to change the preferences in php.ini?

This question is related to php html apache file

The answer is


For combining HTML and PHP you can use .phtml files.


You can modify .htaccess like others said, but the fastest solution is to rename the file extension to .php


In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:

AddHandler application/x-httpd-php .html

AJAX is also a possibility. Effectively you would want data from the php page. Once you have the data you can format in anyway in javascript and display.

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
}

For having .html files parsed as well, you need to set the appropriate handler in your server config.

For Apache httpd 2.X this is the following line

AddHandler application/x-httpd-php .html

See the PHP docu for information on your specific server installation.


By default you can't use PHP in HTML pages.

To do that, modify your .htacccess file with the following:

AddType application/x-httpd-php .html

Add this line

AddHandler application/x-httpd-php .html

to httpd.conf file for what you want to do. But remember, if you do this then your web server will be very slow, because it will be parsing even static code which will not contain php code. So the better way will be to make the file extension .phtml instead of just .html.


If you only have php code in one html file but have multiple other files that only contain html code, you can add the following to your .htaccess file so it will only serve that particular file as 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>

This will make the PHP executable ONLY on the "yourpage.html" file and not on all of your html pages which will prevent slowdown on your entire server.

As to why someone might want to serve php via an html file, I use the IMPORTHTML function in google spreadsheets to import JSON data from an external url that must be parsed with php to clean it up and build an html table. So far I haven't found any way to import a .php file into google spreadsheets so it must be saved as an .html file for the function to work. Being able to serve php via an .html file is necessary for that particular use.


MODERN REVISION: you also now need to fix it by changing the little known ''security.limit_extensions' in php-fpm doing this. You will probably already know the well documented mechanism of changing apache to AddHandler/AddType I will not go into that here.

  1. YOU MUST find out where php-fpm/conf is in your set up. I did it by doing this

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

  2. I got this back

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

  3. go to this file, edit it and MAKE SURE YOU REALISE IT IS COMMENTED OUT EG: change it from ';security.limit_extensions = .php .php3 .php4 .php5 .php7' <- NB: note the ";" - this line is actually DISABLED by default - and you do not need all the nonsense extensions, in fact they are probably dangerous.. change it to 'security.limit_extensions = .php .htm' <- note the semi colon is now removed. then restart apache/(or nginx) and restart php-fpm # service php-fpm restart # service httpd restart


Create an empty file using notepad and name it .htaccess ,Then copy that file in your project directory and add this line and save.

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

else save the .html file using .php as php can support html,and save it in computer/var/www/html path(linux)


I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??

Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:

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

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?