Include PHP file into HTML file

267,937

Solution 1

In order to get the PHP output into the HTML file you need to either

  • Change the extension of the HTML to file to PHP and include the PHP from there (simple)
  • Load your HTML file into your PHP as a kind of template (a lot of work)
  • Change your environment so it deals with HTML as if it was PHP (bad idea)

Solution 2

Create a .htaccess file in directory and add this code to .htaccess file

AddHandler x-httpd-php .html .htm

or

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

It will force Apache server to parse HTML or HTM files as PHP Script

Solution 3

You would have to configure your webserver to utilize PHP as handler for .html files. This is typically done by modifying your with AddHandler to include .html along with .php.

Note that this could have a performance impact as this would cause ALL .html files to be run through PHP handler even if there is no PHP involved. So you might strongly consider using .php extension on these files and adding a redirect as necessary to route requests to specific .html URL's to their .php equivalents.

Share:
267,937
AlexWillis21
Author by

AlexWillis21

Updated on July 09, 2022

Comments

  • AlexWillis21
    AlexWillis21 almost 2 years

    I'm working on a project that may have to change the same content on all html pages. So I figured I would create a php file and only have to change that so it changes on all pages over the web.

    The files are saved as:

    index.html
    number.php
    

    EXAMPLE:

    ------------------------(HTML FILE)----------------------------

    <html>
       <head>
          <title>Home</title>
       </head>
       <body>
          <h1>Phone Number</h1>
          <?php include('number.php') ?>
       </body>
    </html>
    

    ------------------------(PHP FILE)----------------------------

    <?php
       echo 4895553268;
    ?>
    

    What could I do without changing the file extension of all my html's into php. I've found that works but I would like to only change the code in the html page. I've tried include require tags and that didn't work so I tried script tags and can't seem to make it work right.

  • miky
    miky over 9 years
    second "comnmand" works for me,thx