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!
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?
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.
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.
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.
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