21

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.

Leigh
  • 28,765
  • 10
  • 55
  • 103
AlexWillis21
  • 341
  • 1
  • 2
  • 6
  • You can change how the server serves up HTML files by changing htaccess. – putvande Nov 20 '13 at 19:25
  • See http://stackoverflow.com/questions/7181853/parse-html-as-php – jszobody Nov 20 '13 at 19:27
  • How on earth was this flagged as a duplicate? These are entirely different questions, regardless of the usefulness of the answers. I do not see include anywhere in that question, and it is a stretch to say that the use of include inside a fragment is an obvious extension of the questtion. – mckenzm Feb 21 '22 at 22:35
  • @mckenzm Ya, it beats me, I am having an issue with this site on the blender software side. All the mods have too much power, and almost every post I make there gets flagged as a duplicate. I just think not one single person should have the power to close, or mark as duplicates or anything else the mods can do. And I think the owner of the question should have some say before a decision is made to explain why the question shouldn't be closed or marked as a duplicate. Because I'm tired of checking for a reply and find no one was able to give an answer because a mod has power. – AlexWillis21 Feb 25 '22 at 02:15

4 Answers4

17

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)
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
11

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

Om Bissa
  • 148
  • 1
  • 2
  • 8
2

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.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

You'll have to configure the server to interpret .html files as .php files. This configuration is different depending on the server software. This will also add an extra step to the server and will slow down response on all your pages and is probably not ideal.

McAden
  • 13,714
  • 5
  • 37
  • 63