How do I execute PHP code in a HTML page?

Category: How To

sandiego
Asked:
2012-02-11 7:18 pm EST

Hits: 8,474
Ultimately, I'd like to insert a short line of PHP to get the current date and format it a specific way.

Right now, if I put PHP into my HTML it sends the PHP to the browser!

I need the server to run the PHP and insert the output into my HTML. What do I need to do?

For example, I added two lines to the 'default.htm' that came out of the box. First line is "TESTING..."
Second line is


TESTING...





When I refresh the page, "TESTING..." shows up, but not "HELLO". If I use the browser to inspect the page source, I see my PHP code.

Thanks!

You must login before you can ask a follow up question.

You must login before you can submit an answer.

Best answer chosen by User

0

Tim S.
Staff
9,967 Points
2012-02-13 10:39 am EST
Hi sandiego,

Thanks for posting your question. I'm more than happy to assist you today. You have two options when facing the problem that your PHP code is not executing in an HTML page. Since the web server thinks the page is written in HTML, it does not know to execute the PHP code. You can save the page as a .php file and this should resolve the issue.

Alternatively, if you have a alot of links to that page, changing the file extension will break all the links. You can tell the server in your .htaccess file to execute all HTML pages as though they are PHP. Here's the code to do that:

AddType application/x-httpd-php .html



This tells the server to execute all files with the .html extension as a PHP file.

If you are using the .htm extension on your files you'd add this code:

AddType application/x-httpd-php .htm



Lastly, if you wanted to execute just one file that's got the extension of .html or .htm as a PHP file you could add this line of code:

<Files yourpage.html>
AddType application/x-httpd-php .html
</Files>



Remember, you'll need to replace the yourpage.html with your actual file name. If you need further assistance please feel free to contact us.

Thanks!

Tim S

You must login before you can post a comment about this answer.

The above didn't work for me. However this did:
AddHandler application/x-httpd-php5 .html .htm
billj9000
3 Points
2013-04-13 9:10 am EST
Like this Question?

Related Articles

It looks like there are no related articles.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!