98

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?

John Conde
  • 207,509
  • 96
  • 428
  • 469
Hoon
  • 1,511
  • 4
  • 15
  • 19
  • 3
    You would have to change your server (I assume apache) to serve html files as php, but why do you want the extension to be html instead of php? – Explosion Pills Jul 03 '12 at 13:46
  • 1
    http://php.about.com/od/advancedphp/p/html_php.htm – undone Jul 03 '12 at 13:47
  • @ExplosionPills I've done it using IIS v7! and it's possible! – undone Jul 03 '12 at 13:48
  • @Death I didn't mean to imply that it can *only* be done with apache, I just assumed he was using apache. – Explosion Pills Jul 03 '12 at 13:50
  • In fact, I changed my server recently and, my previous server, they provided php on html but the current server didn't and I was curious. – Hoon Jul 03 '12 at 13:56
  • @ExplosionPills, I really appreciate your attention for my curiosity . But my point was just to solve my issue and I don't care the way. I assumed I've to change my configuration and I couldn't think adding on '.htaccess' at all. Really Thanks for helping me :) – Hoon Jul 03 '12 at 14:02
  • I imagine @Hoon might want to have the file be named .html for a variety of reasons including SEO purposes than have a .php extension. – Edward Jul 06 '13 at 05:07
  • @ExplosionPills I cannot change my homepage's extension from `.html`, unlike every other page I create. – Daniel Springer Apr 20 '17 at 18:05
  • 1
    What a horrible idea. Use redirects to save your SEO. Use .html for HTML and .php for PHP. – Alien Technology Jun 17 '17 at 16:12

12 Answers12

146

You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

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

This will tell Apache to process files with a .htm or .html file extension as PHP files.

John Conde
  • 207,509
  • 96
  • 428
  • 469
  • 15
    While this is *right* I think a more important question is *why* do this? Moreover, he should update server config rather than use `.htaccess` if that's an option. – Explosion Pills Jul 03 '12 at 13:51
  • 11
    I've done this on numerous occasions to hide the fact that I'm using PHP (in addition to other things) – nickb Jul 03 '12 at 13:52
  • 3
    @nickb - security through obscurity? – Nathan Long Jul 03 '12 at 17:56
  • 2
    @NathanLong http://www.w3.org/TR/cooluris/ Specifically, "Keep implementation-specific bits and pieces such as .php and .asp out of your URIs, you may want to change technologies later." in section 4.5. – Eric Finn Jul 03 '12 at 20:12
  • 4
    @EricFinn - that's a good guideline. But having the server process `.html` files as PHP isn't necessary even, or helpful, in following it. You want users to visit `example.com/foo`. You could use that URL to serve PHP content regardless of the file names on your server. If users already have `foo.html` bookmarked, you could still serve `foo.php` without renaming the file. – Nathan Long Jul 03 '12 at 21:34
  • @NathanLong That's fair, I suppose. But my point was that there are other reasons than 'security' for hiding the fact that PHP is being used. – Eric Finn Jul 04 '12 at 03:05
  • 1
    now when I open that .html file then it pops up and wants me to download it. – Kaspar L. Palgi Jun 29 '16 at 12:06
  • 1
    WARNING- I confirm what many have pointed out, apparently long ago (and now in 2019 as of this comment). This change will cause Firefox browsers to offer the visitor a chance to download the page. Even if its fixed in SOME versions of Firefox, I don't think anyone should take a chance! – Randy Jan 27 '19 at 21:56
  • Can we tell that doing this is bad practice? – carloswm85 Nov 15 '19 at 12:56
  • So can I put HTML in `.php` files? – fasterthanlight Aug 06 '20 at 21:44
  • @fasterthanlight Sure. Yes – The concise Aug 29 '20 at 11:50
20

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]
David Morales
  • 17,105
  • 11
  • 72
  • 98
  • What if i use PHP + HTML? which format should i use? I'm quite confused. My page has really short code based on PHP and there are many codes in HTML. So what i was going to do is to make a HTML file. Is this a bad habit? – Hoon Jul 03 '12 at 14:04
  • 3
    The format should be .php, as .html files are intended to contain only HTML code. – David Morales Jul 03 '12 at 14:10
15

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
Chris Trahey
  • 17,799
  • 1
  • 37
  • 51
  • would you care to comment on the difference between ``AddHandler`` and ``AddType``? Thanks. – PatrickT Jan 22 '20 at 03:25
  • 1
    @PatrickT Simplest way to compare is that AddHandler is an "input" mapping - it maps an incoming file extension to a handler. AddType modifies the output MIME type header. See the note here: http://httpd.apache.org/docs/2.4/mod/mod_mime.html#addtype – Chris Trahey Jan 23 '20 at 17:23
13

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

10

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.

Sirko
  • 65,767
  • 19
  • 135
  • 167
Badar
  • 1,312
  • 13
  • 18
7

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.

Sirko
  • 65,767
  • 19
  • 135
  • 167
6

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
Nick
  • 5,978
  • 2
  • 27
  • 45
Kerry Ritter
  • 921
  • 4
  • 14
  • 25
4

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.

MistyDawn
  • 676
  • 7
  • 8
4

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)

Progga Ilma
  • 400
  • 2
  • 6
1

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

Mr Heelis
  • 2,368
  • 4
  • 19
  • 30
0

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
}
-2

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