Okay, so I'm very new to website design, so please keep that in mind. I've been working on a website (with file extension .shtml), and the time has come to add php code to it. It's only a little code, but it doesn't work on the .shtml website; the php code ends up in the source code as a comment. I was wondering if there was a way to make php code work on an shtml website. Thanks.
-
Do you have a PHP server running? – Waleed Khan Jul 18 '12 at 19:11
-
Yes, I have a PHP server running. It was only because it was a .shtml file that the PHP code wasn't working. – user1535846 Jul 18 '12 at 19:18
-
If you've answered your question, you should post how you solved it and mark it as your answer. – Waleed Khan Jul 18 '12 at 19:19
-
I haven't yet answered my question. I was merely stating that the problem didn't lie in the lack of a PHP server. Sorry for the misunderstanding. – user1535846 Jul 18 '12 at 19:22
-
What webserver are you running? Apache? – BOMEz Jul 18 '12 at 19:31
3 Answers
Change the .shtml to .php update the include files you probably have and your on your way.
To keep your .shtml add this
AddType application/x-httpd-php .shtml
Keep in mind this will put additional load on your webserver as it will now scan all .shtml for php instead of just .php files
This can be added in the httpd.conf file
-
`AddType ...` should be added to a .htaccess file in the root of your website. Create it if it doesn't exist. – Jason Allen Jul 19 '12 at 15:10
Write your PHP code as a separate .php file and then include it using SSI, as so:
<!--#include virtual="/path/to/yourfile.php" -->
This worked for me on a hosted website with no changes to .htaccess required. The PHP executes before being included in the .shtml page.
You'll need to setup a web server to get this working. Checkout Apache, or http://www.apachefriends.org/en/xampp.html for something very easy to setup. You can't just drop PHP into an html page and expect it to execute, it needs the interpreter.

- 2,261
- 4
- 30
- 56
-
Yes, I have a PHP server running. It was only because it was a .shtml file that the PHP code wasn't working. – user1535846 Jul 18 '12 at 19:18
-
If you want to maintain the .shtml extension, take a look at mod_rewrite. – Julio Jul 18 '12 at 19:18
-
Yes, I'd like to maintain the .shtml extension, but I don't know about mod_rewrite. I take it there's no way to say on the page itself "make this .shtml page understand PHP"? – user1535846 Jul 18 '12 at 19:24
-
If i'm not mistaken, the interpreter will only interpret .php files, unless you explicitly tell it to interpret other file extensions. I believe you can do something like: "AddHandler php-cgi .shtml" in your .htaccess file. Not sure, going off memory; been a while since i've done any PHP. – Julio Jul 18 '12 at 19:32