Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Write PHP in HTML file

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

The best regards, JC
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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.



ASKER CERTIFIED SOLUTION
Avatar of twohawks
twohawks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
Avatar of Pedro Chagas

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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.

Avatar of njain1985
njain1985

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.
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
@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
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.
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
Hey joaochagas, thank you for the points assignment.  I hope you finally got things worked out positively.