Running php script in HTML files

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Running php script in HTML files

Postby AlecS » 01. September 2009 13:46

Hi

I've just loaded the latest XAMPP package onto a new Windows XP Professional machine. I've done a test.php in the localhost and that works.

How do configure XAMPP to run php script in files with a .html extension?

Alec
AlecS
 
Posts: 4
Joined: 01. September 2009 13:25

Re: Running php script in HTML files

Postby sili » 01. September 2009 13:51

That really doesn't make any sense ;)

Anyway: Create a file called .htaccess, save it to your root and add this into that file:
Code: Select all
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
sili
 
Posts: 54
Joined: 15. March 2003 16:05
XAMPP version: dfg
Operating System: dfg

Re: Running php script in HTML files

Postby Nobbie » 01. September 2009 13:55

AlecS wrote:How do configure XAMPP to run php script in files with a .html extension?


Why do you need that? You can do that (its not that difficult), but it is a bad idea, because it means that PHP is always invoked when a HTML file is requested - even if there is no PHP inside that HTML file (what should be the standard case).

You may include() HTML files from PHP files, in that case the HTML File will be parsed as well. But to parse HTML in any case is an overkill and i cannot find any sense in doing so.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: Running php script in HTML files

Postby AlecS » 01. September 2009 16:02

Thanks for your suggestions. It may seem a bad idea to run php script in HTML files, but my web sites are generate using an existing application that requires files with a .html extension. Hundreds of file are involved. Also these files are well indexed on the internet. All these files have php scripts running in them and the whole lot works really well on the server.

The problem is the latest XAMPP installed on the new computer. My old computer works with an older XAMPP package which handles running php scripts in HTML files. I assumed that I could configure the latest XAMPP the same as the old XAMPP package, but the content in the configuration files are different between the two packages.

I've tried a .htaccess file in the root. but that didn't work.

Surely there must be way to run php script in HTML files in the latest XAMPP like on my other machine?

Alec
AlecS
 
Posts: 4
Joined: 01. September 2009 13:25

Re: Running php script in HTML files

Postby Nobbie » 01. September 2009 16:17

AlecS wrote:My old computer works with an older XAMPP package which handles running php scripts in HTML files


No, Xampp never runs HTML files thru PHP. You (or someone) must have changed the server configuration.

AlecS wrote:I've tried a .htaccess file in the root. but that didn't work.


Probably due to a missing (or wrong configured) Option "AllowOverRide". You should insert the Option "AllowOverRide All" to <Directory ..>-Block of your DocumentRoot.

Or simply directly edit httpd.conf and modify the line from

Code: Select all
AddType application/x-httpd-php .php


to

Code: Select all
AddType application/x-httpd-php .php .html


or similar (I dont know your configuration file), you simply have to add the extension .html to the AddType-statement of .php extension. If you do so, you dont need .htaccess file instead.

And dont forget to restart Apache after changing httpd.conf
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: Running php script in HTML files

Postby AlecS » 01. September 2009 17:00

Hi

This line of code 'AddType application/x-httpd-php .php .html' is in the httpd.conf file. Also. AllowOverRide All is there too.

Yes I did configure the old computer, but can't remember what I did because it was a long time ago. As I said above the two systems are different. Certainly the contents of the httpd-xampp.conf file is not same as the one on the old computer.

Is it possible to copy the old XAMPP system over to the new computer?

Alec
AlecS
 
Posts: 4
Joined: 01. September 2009 13:25

Re: Running php script in HTML files

Postby Wiedmann » 01. September 2009 17:15

This line of code 'AddType application/x-httpd-php .php .html' is in the httpd.conf file.

This line does not exists in the original "httpd.conf" from XAMPP. Please remove it.
(And maybe other changes you have made without knowledge, too.)


How do configure XAMPP to run php script in files with a .html extension?

In the file "\xampp\apache\conf\extra\httpd-xampp.conf" (line ~21) change:
Code: Select all
<FilesMatch "\.php$">


to:
Code: Select all
<FilesMatch "\.(php|html)$">


and restart Apache.
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: Running php script in HTML files

Postby AlecS » 01. September 2009 19:41

Success! Many thanks for solving this problem. I think I'd spent quite a few hours on it so far. I was getting really down about it.

Thanks again

Alec
AlecS
 
Posts: 4
Joined: 01. September 2009 13:25

Re: Running php script in HTML files

Postby bebear » 10. September 2009 10:19

hi, i did the same by putting in the .htm or .html in the field

<FilesMatch "\.(php|html|htm)$">

but it didn't work for me.

could someone please help me on this? what else i can do?

thanks in advance ;-)
bebear
 
Posts: 2
Joined: 26. August 2005 03:19

Re: Running php script in HTML files

Postby Mobius » 04. March 2010 03:33

I have been trying to understand how to parse php in html files.
I want to use php to display the same menu in all my html files.
If there is a better way, I am open to hearing about it.
I do NOT want to use javascript.

I understand that I need to add the following line to: httpd.conf

"AddType application/x-httpd-php .php .html .htm" (without the quotes)

But, I do not know where in httpd.conf to add that line.

On my website, which is linux, it is done in the .htaccess file, but on my windows localhost there is no .htaccess file.

I have put the "AddType..." line in various places in the httpd.conf file, but nothing works.

My windows localhost is just for doing local testing, so any overhead is OK since I only run xampp/apache while testing.

Should it go in the httpd-xampp.conf file?
Mobius
 
Posts: 8
Joined: 24. February 2010 00:48

Re: Running php script in HTML files

Postby cthackers » 04. March 2010 12:10

I think AlecS was trying to ask about .html seo optimized links. Requests with .html extensions sent to php files because of ModReqrite rules. That makes more sense than embedding php in html static files and making htmls beeing parsed by php interpreter.
cthackers
 
Posts: 14
Joined: 03. March 2010 20:06

Re: Running php script in HTML files

Postby Mobius » 07. March 2010 22:33

cthackers wrote:I think AlecS was trying to ask about .html seo optimized links. Requests with .html extensions sent to php files because of ModReqrite rules. That makes more sense than embedding php in html static files and making htmls beeing parsed by php interpreter.


Well, I did not see anything in his message about SEO, but since it took me several readings of this thread to finally find the answer to my question, I probably missed it.

I am posting, in this message, what it was that finally made it so that XAMPP/Apache now parses PHP code inside of a HTML file. 99% of my HTML files have PHP code in them, so the argument about "this is not the way you should do it" does not seem to hold water to me. Plus, as I said before, this is a localhost installation only for the purposes of testing my files. Since XAMPP/Apache only runs when I need it, any extra overhead created by making the change below is more than tolerable. BUT, I have not seen any slowdown of my computer, so I really don't get the argument.

1) In c:\xampp\apache\conf\extra find the file: httpd-xampp.conf and open it in your text editor (I like Notepad++).
2) Find the entry: <FilesMatch "\.php$">
3) Comment that entry (add the hash mark before it).
4) Add the following entry below it: <FilesMatch "\.(php|html|htm)$">
5) Save the file, and restart XAMPP and Apache.

The resulting change will look like this:

# bookmark1 (I add this before any change I make so I find changes easily)
# <FilesMatch "\.php$">
<FilesMatch "\.(php|html|htm)$">

Now, my Apache installation recognizes PHP code inside of my HTML files, and passes the PHP code to the PHP interpreter.

I am in no way an expert, but am just now learning how to do this, so I probably will not be able to answer any questions about PHP, Apache, or XAMPP.

However, I will ask the 'why' question, so maybe a real expert can answer it for me. Why is this a "bad" idea? A detailed explanation, please. Or, better yet, point me somewhere that I can read an explanation.

Mainly I am using PHP to call an external file as a menu. Is there a better way of doing this? I am helping support a church website and wanted to make it easy for them to maintain. This method will make it easy for them to modify one file, the external menu file, in order to change the menu on every page on their website. Plus they can delete and add pages without too much difficulty.

.
Mobius
 
Posts: 8
Joined: 24. February 2010 00:48

Re: Running php script in HTML files

Postby Altrea » 07. March 2010 22:46

Other way round:
If your files contain php code, why don't you give them the php file extension?
This is the right way to do this!

It's absolutely okay, to outsource code parts like a navigation menu. But to route all htm files through the php interpreter is not the clean way.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Running php script in HTML files

Postby Mobius » 07. March 2010 23:11

Altrea wrote:Other way round:
If your files contain php code, why don't you give them the php file extension?
This is the right way to do this!

It's absolutely okay, to outsource code parts like a navigation menu. But to route all htm files through the php interpreter is not the clean way.


99% of the HTML files have the "outsource code" to the "navigation menu". *_I_* understand that it could all be done in PHP, (I think the >>>HEREDOCS is what you are getting at?) however, my user only has MS Frontpage. I don't. So, can Frontpage handle PHP files? I am not about to try to teach them something like KompoZer (which I have not yet tried to edit a PHP file with yet, so I do not know if it can handle PHP either). That is way above my pay grade, which is zero. They are getting by, but I am trying to help them be able to maintain their website a little easier.

I am willing to read about the issue, but I do not know what to read.
Mobius
 
Posts: 8
Joined: 24. February 2010 00:48

Re: Running php script in HTML files

Postby Altrea » 07. March 2010 23:52

Mobius wrote:99% of the HTML files have the "outsource code" to the "navigation menu".

So, 99% of the files can have a php file-extension.
If 99% of your image files on your computer are jpegs, you don't try to rename the 1% gifs to jpeg and train your image program to handle them like gifs, do you?

Mobius wrote:*_I_* understand that it could all be done in PHP, (I think the >>>HEREDOCS is what you are getting at?)
HEREDOC is a nice way for some string situations. For Navigations, i don't know.

Mobius wrote:however, my user only has MS Frontpage. I don't. So, can Frontpage handle PHP files?

I don't know if Frontpage can handle php files. I don't really like Frontpage :D
Does your user uses Frontpage as WYSIWYG Editor or text Editor? php-files are simple text files, so every plain text editor can edit them, and there are some other Editors out there in the internet (like notepad++).

However, if you really want html-files your user can change in frontpage, why don't you create a template-/front-controller-php file? You can include in this file html files (e.g.: page-header, navigation, content ,page-footer) which can be edited with html editors like frontpage.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 124 guests