Running PHP within HTML pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Running PHP within HTML pages

Post by ERdman »

i posted to an older thread but didn't get any responses yet, so I am going to make this quick. I have worked with PHP as a CGI script language, but haven't begun trying to embed it in HTML pages until now (because I am trying to start using session-tokens to avoid some common security issues)... SO the problem I am having is that embedded PHP will not work unless I save the page as a .php file. Is this the way it has to be? I can't use index.html and run embedded PHP scripts? I have tried

<script language ="php"> echo "hello world."; </script>
as well as
<?php echo "hello world."; ?>
and
<?= echo "hello world."; ?>
and
<% echo "hello world."; %> (with the proper adjustments made to php.ini to allow use of this tag)

but to no avail. They only work when I use pagename.php not pagename.html. Am I missing something?

Thanks for any help...! :banghead:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Running PHP within HTML pages

Post by Celauran »

Try adding this to your .htaccess file

Code: Select all

AddType application/x-httpd-php .html
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: Running PHP within HTML pages

Post by ERdman »

Celauran wrote:Try adding this to your .htaccess file

Code: Select all

AddType application/x-httpd-php .html
Celauran-
Wow that was easy! It worked for my "hello world" example - now I'm going to try testing session tokens, etc. I appreciate the help! :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Running PHP within HTML pages

Post by pickle »

That line tells Apache to treat all html files as PHP files. You can do the same with .shtml or .phtml if you wanted.

Know though, that this results in the PHP interpreter being invoked & every HTML file being parsed. This causes a relatively large amount of overhead when compared to Apache simply opening the HTML file & sending it to the browser. This will almost certainly not be a problem with the low amount of usage you'll have, but I thought you should be made aware of exactly what's going on.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: Running PHP within HTML pages

Post by ERdman »

pickle wrote:That line tells Apache to treat all html files as PHP files. You can do the same with .shtml or .phtml if you wanted.

Know though, that this results in the PHP interpreter being invoked & every HTML file being parsed. This causes a relatively large amount of overhead when compared to Apache simply opening the HTML file & sending it to the browser. This will almost certainly not be a problem with the low amount of usage you'll have, but I thought you should be made aware of exactly what's going on.
Is there an alternative method to get the server to execute php within html, or is this how it works for most servers? Also, I noticed that on pages named PAGE.html php now works but on pages named PAGE.htm it doesn't. (Don't ask why some some pages are html and others htm, it happened by mistake). Do you think it would be better to just rename the pages as .html or to add another line to the htaccess file (if that will even work)? Thanks again.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Running PHP within HTML pages

Post by s992 »

You can add another line to .htaccess for htm extensions.

However, why not just use the .php extension on your files? You can still use normal HTML code in the files and you won't need to mess with .htaccess.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Running PHP within HTML pages

Post by Luke »

To add to pickle's response, I would just like to say that although it is often said that PHP can be embedded into HTML, it is actually, in reality, the other way around. HTML can be embedded in PHP files. Like pickle said, by adding that line to your .htaccess file, you are telling apache to treat all files ending in .html as PHP pages. This means that all of your HTML pages are actually now PHP pages with an "html" extension. I have to ask, why exactly do you want your html files to be interpreted as PHP? Why not just change them to .php pages? Is it because there are just too many places you'd have to do this? Just curious.
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: Running PHP within HTML pages

Post by ERdman »

Luke wrote:To add to pickle's response, I would just like to say that although it is often said that PHP can be embedded into HTML, it is actually, in reality, the other way around. HTML can be embedded in PHP files. Like pickle said, by adding that line to your .htaccess file, you are telling apache to treat all files ending in .html as PHP pages. This means that all of your HTML pages are actually now PHP pages with an "html" extension. I have to ask, why exactly do you want your html files to be interpreted as PHP? Why not just change them to .php pages? Is it because there are just too many places you'd have to do this? Just curious.
I want to run php in my index.html page (and other pages) without having to forward to a .php file. The reason is that we are implementing session tokens for user-submitted form security (which happens to exist on every page). I dont mind renaming all the pages with the .php extension, though it will take time to fix all the links, but it seems like a pain to have to redirect users from the index.html page to a .php page just to implement a two-line embedded php script. Or maybe I'm wrong...? -Erd
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Running PHP within HTML pages

Post by Celauran »

ERdman wrote:it seems like a pain to have to redirect users from the index.html page to a .php page just to implement a two-line embedded php script.
There's no need for redirection. Embed the php code in the index.html page, rename to index.php, you're done.
Post Reply