• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Write PHP in HTML file

Hi E's, is possible write PHP in html or htm file?

The best regards, JC
0
Pedro Chagas
Asked:
Pedro Chagas
  • 6
  • 4
  • 2
  • +2
2 Solutions
 
Hugh McCurdyCommented:
I'm not sure what you mean.

Do you mean you want to have PHP code inside an HTML file and have the PHP processor process it?  I think it's possible but even if it is, I strongly recommend against it.  Otherwise, I don't understand the question.  Perhaps if you explain why you want to do this, I'll be better able to help you.

What you normally do is have a file that has a PHP extension.  For instance, instead of index.html you would have index.php.  You can actually write in just HTML code inside a PHP file.  It just takes a little longer to serve the page because the PHP interpreter will scan the file only to discover it has nothing to do.



0
 
twohawksCommented:
Yes.  You simply need to tell the server to parse htm / html files for php.

For instance, one way to do this in an apache environment that allows you to use htaccess files is

Create a file named ".htaccess" in your root directory
in that file place the following command:

AddHandler application/x-httpd-php5 .html .htm

*OR

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

*OR

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

**Differnt hosts have differnt deployments and/or limits -- that's why the 'OR's
I recommend looking up some, and reading a little about, this and trying out
0
 
twohawksCommented:
You need to know that this tells the server to parse all files with the extension you specify, and so will incur some additional overhead.  Not a big deal in small sites, and it is not uncommon to do, but in large complex sites it is wise to review the impact implications.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
twohawksCommented:
I left out that some hosts may require two directives together, such as..
AddType application/x-httpd-php .htm .html
AddHandler x-httpd-php  .htm .html
0
 
Pedro ChagasWebmasterAuthor Commented:
I will explain:
I have a project that I write long years ago (about 10y), and all has been write in HTML only.
Some years ago I learn more about PHP, and I think, why not make a update do my old site.
I just want write lines of php in .htm file because I need to use php and because I want maintain the same URL. If I don't wanted preserve the URL, is simple, just change the file from name.htm to name.php.
The priority is I can writhe php inside HTML, and very important is preserve the actual URL!

The best regards, JC
0
 
jan-janssenCommented:
Obviously this is not easy at all as by definition a server executes a *.html file as static html and an *.php file as php. To fix your issue anyway there are some workarounds, as long as your host supports .htaccess files (apache) :

create an .htaccess file including
RewriteEngine on
RewriteRule index\.html index.php [NC,R]

Open in new window

For the full explanation check http://css-tricks.com/377-how-to-redirect-indexhtml-to-indexphp/ 

or create an .htaccess file and include:
AddType application/x-httpd-php .html .htm

Open in new window

As seen on http://www.besthostratings.com/articles/php-in-html-files.html 

If your hosting provider is not using Apache, please post your web server details.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi, thanks for the answers.
I try all the solutions and no work! I believe the problem it is the server where I have my site.
Because that I contact my hosting support to know what they say about this situation.
So, I come back to this question when they give me a answer.
Again, thanks for your answers.

The best regards, JC
0
 
Hugh McCurdyCommented:
OK.  I get it now.  I withdraw my recommendation against it.  I see lots of fine solutions and see no point in duplicating them.  Good luck.
0
 
twohawksCommented:
Yeah JC, if your site hosted on Win IIS, then, unless you have a control panel provided by the host for doing so yourself (if you have a Control Panel login go look arond in there for mappings), then you will need to request that they map "the PHP executable they are using" to .html files (or what you want)  for your site.  Reason we ask over 'what php exec they are using' is we don't know if its CGI (unlikely), fastCGI (possible) or php_isapi.

0
 
njain1985Commented:
Hi joaochagas,

The answer to your question is simple.
Just include all your php codes within your html
document by adding
<html>
<head>
</head>
<body>
<?
---all your php codes go here,
furthermore, you can include these
tags anywhere in a html document.
?>
</body>
</html>

This is the most common way html includes php.
Hope your question is answered.
0
 
njain1985Commented:
Dear joaochagas,

Furthermore, you can save your file as .html/.htm and
your php codes will more than execute well.

All the best,
Nikunj Jain
0
 
Pedro ChagasWebmasterAuthor Commented:
@twohawks, I don't have any cpanel, I don't have e-mail, I request password some days ago and no body give me, I write yesterday a email about this issue, and the answer is nothing. 10y ago I do a bad business when I buy this hosting (today the situation is different). But I think the OS is UNIX!

@njain1985, if it was so simple, I'm not here!

Regards, JC
0
 
twohawksCommented:
Does your host provider have a website, and also possibly a forum?

1) If they have a website, perhaps you can find a form on the site for contacting them directly instead of by emailing them ?

2) If they have a forum, then sign up and talk to some of your peers there... perhaps someone can point a way to help with the host.

3) Obviously, if you are not having support with your host you should move hosting somewhere else.  There are just too many other choices out there for good webhosting at a good cost to stay with a "broken" webhost.

If you have been venturing into php, and if you choose to move to another host, I highly  recommend you be sure to get a plan setup on a linux-apache platform, not windows IIS or (windows, period), for the best web developer experience.

Moving your site should not be that big a deal.
I don't know how else you can tackle this beyond the three options offered up here
 - htaccess addhandler method for parsing html pages for php
 - htaccess modrewrite method for redirecting pages (that jan posted ;^)
 - remapping method available on win IIS, but usually your host has to do that unless you have a control panel *(and I don't think that's technically a "cpanel", but its a control (user admin) panel anyway).


That's all I got.  Keep us posted.
0
 
Pedro ChagasWebmasterAuthor Commented:
Hi @twohawks, they have a website and I send a email, and after 24 hours I don't have any answer, I reinforce with a new e-mail today night. Maybe tomorrow, if I have luck, I receive a email with some answer.

I try to call and they phone don't work. They don't have a forum. They don't give access to cpanel.

About changing the host, I should have done long ago, but fortunately I have a lot of work in this area and have delayed the transfer of server, including currently have a dedicated server (linux-apache platform). In fact, one of the objectives of this question is to make the move to my server, but for that I have access to php. I've tried to redirect it from file. Htaccess, but does not work either. Now I'm trying to get php to write, to make the 301 redirect by HEADER input in pages codes. Also I have prepared now a script for edit all index.htm at the same time.

It seems that now I can only hope.

I appreciate your help, and soon I will try all the codes post here in my dedicated server for make the distribution of the deserved points. That is important too!

~JC
0
 
twohawksCommented:
Hey joaochagas, thank you for the points assignment.  I hope you finally got things worked out positively.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now