Include PHP script inside HTML

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

This is the contents 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>
+3
source share
5 answers

What result do you get? Does it just show PHP code?

I guess that is the case.

For the test, change the file extension to .phpand run it again. He is working now?

If so, you need to associate with PHP files .htmland .htmon your server.

EDIT

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

AddType application/x-httpd-php .html
+7
source

You can embed PHP code directly in HTML using a tag <?php ?>and put your php code between the opening and closing tags.

/etc/apache 2/apache2.conf, php- .html :

AddType application/x-httpd-php .html

+2

php, - PHP ( -), URL .

+1

, php- html (Tested)

PHP , , PHP- *.html *.htm. ".htaccess" . , .htaccess , . , " , -".

, .htaccess . , - HTML, PHP.

".htaccess" . , . Unix- , dot . (. , , "xyz.htaccess" . - , ". Htaccess" ) , "" MS Windows. : AddType/x-httpd-php.html.htm , , PHP5: AddType/x-httpd-php5.html.htm .htaccess -. , ".htaccess" . - *.htm *.html, PHP.

, , HTML , . "php-in-html-test.htm", -:

 <!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>

, : http://www.your-domain.com/php-in-html-test.htm ( , , your-domain.com ...) " !" , PHP . * html *.htm . , , .htaccess, . , .

+1

I am surprised that no one mentioned the way to hack it - if you do not want to make it difficult to change the configuration of Apache, here is how you do it:

In your .html file you just use iframe

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

It will just show what will appear as if you went directly to www.mysite.com/myScript.php

So for example, we could have

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

All Articles