27

How can I run simple PHP code inside a .html file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ABA
  • 435
  • 1
  • 5
  • 13

8 Answers8

9

To execute 'php' code inside 'html' or 'htm', for 'apache version 2.4.23'

Go to '/etc/apache2/mods-enabled' edit '@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'

Script47
  • 14,230
  • 4
  • 45
  • 66
7

You can't run PHP in an html page ending with .html. Unless the page is actually PHP and the extension was changed with .htaccess from .php to .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";?>
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • Not exactly, you're near the ballpark but not hitting the nail right on the head, as it were. – Funk Forty Niner Apr 04 '14 at 04:40
  • 4
    The link http://php.about.com/od/advancedphp/p/html_php.htm that ProllyGeek provided explains the "why". The OP says in the question *"how to run a simple php code inside a **.html** file extension"* --- It's a specific question. – Funk Forty Niner Apr 04 '14 at 04:54
6

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.

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!

ABA
  • 435
  • 1
  • 5
  • 13
3

Simply you cant !! but you have some possbile options :

1- Excute php page as external page.

2- write your html code inside the php page itself.

3- use iframe to include the php within the html page.

to be more specific , unless you wanna edit your htaccess file , you may then consider this:

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

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
  • 1
    I... don't think so. [**Read my comment**](http://stackoverflow.com/questions/22853669/how-to-run-a-php-script-inside-a-html-file#comment34864358_22853669) and re-read OP's question carefully. Plus, not all servers will run PHP inside regular HTML files; I know this from experience. – Funk Forty Niner Apr 04 '14 at 04:45
  • There is indeed a problem. Nobody so far has gotten it right, except me. – Funk Forty Niner Apr 04 '14 at 04:47
  • @Fred-ii- would you please tell me what is the problem , so i can think of a solution ?! – ProllyGeek Apr 04 '14 at 04:48
  • Google my comment under OP's question and you will find the right results. Starting from Apache... – Funk Forty Niner Apr 04 '14 at 04:48
  • You're partially right about the "can't", but it's not impossible. – Funk Forty Niner Apr 04 '14 at 04:50
  • i didnt say you were mistaken fred , but the user seems not be professional , you should choose right answer , adn right comment , there are more than way to do , and i have just mentioned the simplest. – ProllyGeek Apr 04 '14 at 04:50
  • @Fred-ii- you may consider adding an answer , and it is up to OP to choose the correct answer. – ProllyGeek Apr 04 '14 at 04:51
  • 1
    It's usually best to tell the OP the "why" an `.html` file extension can't run PHP code. However, the link you provided does explain it, yet that should have been "your" job ;-) – Funk Forty Niner Apr 04 '14 at 04:51
  • 2
    I gave an example in my post, check it :) – CMPS Apr 04 '14 at 04:52
  • As far as my adding an answer, it's ok. I think you come pretty close to what the OP needs to do. – Funk Forty Niner Apr 04 '14 at 04:52
  • @Fred-ii- be more practical , and add your own answer , may be it will be useful for all of us , even for people who seek answer in future , rare people may read comments , everyone just look up an answer – ProllyGeek Apr 04 '14 at 04:54
  • There are too many possible reasons why doing it may or may not work; I can't go into further details about it; it's far too broad. Sometimes, only adding an AddHandler isn't enough on some systems. – Funk Forty Niner Apr 04 '14 at 04:56
  • I've worked on different types of servers with different configurations, and they're not all the same. – Funk Forty Niner Apr 04 '14 at 04:58
  • @ProllyGeek, I like this for the simplicity of inserting a bit of php through an iframe with little effort and no server/htaccess mods. Any caveats? – aamarks Jan 08 '22 at 18:41
2

Yes, you can run PHP in an HTML page.

I have successfully executed PHP code in my HTML files for many years. (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).

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.

Here is what one host ( http://simolyhosting.net ) support did for me in 2008 -- but it no longer works for me now.

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 )


Below are other solutions I found -- they are NOT MINE:


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. Using cPanel Apache handlers or adding this directly in to .htaccess (same as cPanel does through gui add handlers):

AddHandler application/x-httpd-php5 .html

Sep 9, 2016

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

Open a text editor such as wordpad, notepad, nano, etc. and add the following line:

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:

AddHandler x-mapp-php6 .html .htm

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:

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.

tcloud
  • 51
  • 5
1

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

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

Hope This Helps

  • Yes, I think you got the idea. With my idea I can echoes php body in my index.html page but I can not pass values to be treated on php code. To do that I should find a way to put Javascript variables inside to php. Your suggestion is use javascript to call php. So I can pass variables. It seems to be best practice. – ABA Apr 04 '14 at 07:39
  • yes, you could pass many things from data as data:"param="+var_from_javascript+"&anotherparam="+another_var_from_javascript – Bandeshor Makai Apr 04 '14 at 08:01
  • This worked beautifully for me. I put my php code for obtaining the commit hash and branch name into git-info.php asCommit: $rev    Branch: $branch "; ?> and executed it as above! Thanks! – John Wooten Jan 23 '20 at 18:18
1

I'm not sure if this is what you wanted, but this is a very hackish way to include php. What you do is you put the php you want to run in another file, and then you include that file in an image. For example:

RunFromHTML.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

<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!" inside of it.

K Zhang
  • 269
  • 3
  • 9
  • @Bob Last time I checked, it does. If you want, you can try it out by setting up a local server and running the code. A nice and simple server to use is XAMPP, which you can download [here](https://apachefriends.org/) – K Zhang Aug 23 '16 at 00:28
0
<?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.

James Kidd
  • 444
  • 4
  • 8
  • @simplr I'm not down-voted. Without this line `AddType application/x-httpd-php .php .htm .html` code @James_Kidd not working. – Andrei Krasutski Jun 30 '17 at 08:10
  • Thanks for the hint. I was trying to get the .htaccess setup working and now I realized that my server does not display any PHP at all. I can now move on to look for another solution. Thanks again! +1 – rmbianchi Sep 11 '19 at 16:05