3

I am a newbie and have never used PHP before. I want to execute PHP script from an HTML file on Linux. What do I need to do?

This is the content of my HTML file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Testing Weather Data</TITLE>
   </HEAD>
   <BODY>
      <div id="weather">
         <p>Weather Information
         <?php include "/home/tahoang/Desktop/weatherData.php"; ?>
      </div>
   </BODY>
</HTML>
1
  • You need to update Your Apache configuration, and add .html ad extension for php. Commented Jun 20, 2011 at 19:35

5 Answers 5

9

What output do you receive? Does it just show the PHP code?

I assume that's the case.

For a test, change the extension on the file to .php and run it again. Does it work now?

If so, you will need to associate PHP with .html and .htm files on your server.

EDIT

This is the line you want to add to your Apache config:

AddType application/x-httpd-php .html
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Djdy, it doesn't show anything. Just the plain HTML code. Where can I find the config of Apache? I have Apache 2 running.
I am on Linux (Ubuntu 11.04) with Apache 2
The default is /etc/apache2. Look in httpd.conf file, or sites-enabled
I tried putting AddType application/x-httpd-php .html into httpd.conf and restart the service but it's still not working
Here's a detailed guide to installation, try going over it from the beginning: help.ubuntu.com/community/ApacheMySQLPHP
|
2

You can embed the php code right into HTML using <?php ?> tag and putting your php code between opening and closing tags.

Before that you need to add this handler in /etc/apache2/apache2.conf file to run php scripts inside .html file:

AddType application/x-httpd-php .html

1 Comment

Where to put that handler?
1

You either run pass the file path as an argument to the php command line program, or you configure a web server to execute the page as PHP (the specifics of which depend on the web server you use) then visit the URL of the page.

3 Comments

Hi Quentin, I plan to run apache2, what should I configure? Right now my index.html does not invoke the weatherData.php.
Rename index.html to index.php, don't tell Apache to parse every HTML document for PHP directives.
I am a newbie so I don't quite get the 2d part :(
1

Here is the step by step process to include php code in html file ( Tested )

If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."

You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.

First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file. (Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess") Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file: AddType application/x-httpd-php .html .htm If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5: AddType application/x-httpd-php5 .html .htm Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.

You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 <HTML>
<HEAD>
<TITLE>Use PHP in HTML files</TITLE>
</HEAD>

<BODY>
    <h1>
    <?php echo "It works!"; ?>
    </h1>
</BODY>
</HTML>

Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...) If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.

Comments

0

I am surprised no-one has mentioned a way to hack it - if you don't want to hassle of changing the Apache config here is how you do it:

In your .html file you simply make use out of iframes

<iframe name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>

This will simply just display what ever is displayed as if you went directly to www.mysite.com/myScript.php

So for example we could have

<?php
echo 'Hello World!';
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.