Page 1 of 1

Running PHP within HTML pages

Posted: Mon Nov 15, 2010 10:40 am
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:

Re: Running PHP within HTML pages

Posted: Mon Nov 15, 2010 10:49 am
by Celauran
Try adding this to your .htaccess file

Code: Select all

AddType application/x-httpd-php .html

Re: Running PHP within HTML pages

Posted: Mon Nov 15, 2010 1:35 pm
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

Re: Running PHP within HTML pages

Posted: Mon Nov 15, 2010 1:40 pm
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.

Re: Running PHP within HTML pages

Posted: Wed Nov 17, 2010 3:04 pm
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.

Re: Running PHP within HTML pages

Posted: Wed Nov 17, 2010 3:59 pm
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.

Re: Running PHP within HTML pages

Posted: Wed Nov 17, 2010 8:43 pm
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.

Re: Running PHP within HTML pages

Posted: Thu Nov 18, 2010 11:22 am
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

Re: Running PHP within HTML pages

Posted: Thu Nov 18, 2010 11:50 am
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.