Parsing a php file with html extension

K

kayodeok

Would someone mind clarifying a few points for me:

My webpages in my sig all have the html extension however, I have
one page which should be parsed as php (not yet uploaded).

I do not want to add the php extension preferring to keep the html
extension.

My research on this is quite conflicting:

some Websites such as http://www.spiderpro.com/bu/buphph001.html
asks me to include AddType application/x-httpd-php .php .html in my
..htacess file and put it in every directory I need it for.

Others such as http://www.zend.com/manual/security.hiding.php asks
me to include just AddType application/x-httpd-php .html in my
..htacess file but mentions that I would suffer a performance hit on
my pages.

I am unable to locate the definitive source for these instructions.

Does anyone know where I can locate the source url? Or better
still, explain the difference between

AddType application/x-httpd-php .php .html

and

AddType application/x-httpd-php .html

Thanks
 
B

Bob Long

In
kayodeok said:
Would someone mind clarifying a few points for me:

My webpages in my sig all have the html extension however, I have
one page which should be parsed as php (not yet uploaded).

I do not want to add the php extension preferring to keep the html
extension.

My research on this is quite conflicting:

some Websites such as http://www.spiderpro.com/bu/buphph001.html
asks me to include AddType application/x-httpd-php .php .html in my
.htacess file and put it in every directory I need it for.

Others such as http://www.zend.com/manual/security.hiding.php asks
me to include just AddType application/x-httpd-php .html in my
.htacess file but mentions that I would suffer a performance hit on
my pages.

I am unable to locate the definitive source for these instructions.

Does anyone know where I can locate the source url? Or better
still, explain the difference between

AddType application/x-httpd-php .php .html

and

AddType application/x-httpd-php .html

Thanks

It's probably AddHander that you want:

http://httpd.apache.org/docs/mod/mod_mime.html#addhandler
 
B

Bob Long

In
kayodeok said:
Would someone mind clarifying a few points for me:

My webpages in my sig all have the html extension however, I have
one page which should be parsed as php (not yet uploaded).

I do not want to add the php extension preferring to keep the html
extension.

My research on this is quite conflicting:

some Websites such as http://www.spiderpro.com/bu/buphph001.html
asks me to include AddType application/x-httpd-php .php .html in my
.htacess file and put it in every directory I need it for.

Others such as http://www.zend.com/manual/security.hiding.php asks
me to include just AddType application/x-httpd-php .html in my
.htacess file but mentions that I would suffer a performance hit on
my pages.

I am unable to locate the definitive source for these instructions.

Does anyone know where I can locate the source url? Or better
still, explain the difference between

AddType application/x-httpd-php .php .html

and

AddType application/x-httpd-php .html

The extensions on the end specify which are used. You can have more than
one.
http://httpd.apache.org/docs/mod/mod_mime.html#addtype

Bob Long
(If my other message appears - even though I cancelled it, ignore it. It's
wrong and was sent prematurely.)
 
T

Toby A Inkster

kayodeok said:
AddType application/x-httpd-php .php .html
AddType application/x-httpd-php .html

Assuming the server is already set up to parse ".php" files as PHP, then
both are exactly equivalent.
but mentions that I would suffer a performance hit on
my pages.

This is because it instructs Apache to parse *all* files with the
extension ".html" as PHP. If you just need the one, then you could use
something like:

<Files myscript.html>
ForceType application/x-httpd-php
</Files>

There is still a *tiny* performance hit -- this is because *any*
".htaccess" files need to be parsed for each and every request from the
containing directory or any of its subdirectories.

To get around that, you may find it useful to do this configuration in the
main httpd.conf file, assuming you have permission.
 
K

kayodeok

The extensions on the end specify which are used. You can have
more than one.
http://httpd.apache.org/docs/mod/mod_mime.html#addtype

Thanks
Based on the source document, I was able to construct more accurate
searches to research the document.

Previously, I was searching for "AddType application/x-httpd-php"
which did not return any file from apache.org and I was a bit too
stressed to notice that application/x-httpd-php is a mime type.

Regards
 
E

Eric B. Bednarz

kayodeok said:
Previously, I was searching for "AddType application/x-httpd-php"
which did not return any file from apache.org

Incidentally, that's a good thing, because it's a popular but fatal
misconfiguration of the server; leave AddType alone and locate the
AddHandler directive on the same page of the documentation.

For more info, consult your old friend:

<http://www.google.com/search?q=406+http+accept+addtype>
 
K

kayodeok

Assuming the server is already set up to parse ".php" files as
PHP, then both are exactly equivalent.


This is because it instructs Apache to parse *all* files with
the extension ".html" as PHP. If you just need the one, then you
could use something like:

<Files myscript.html>
ForceType application/x-httpd-php
</Files>

There is still a *tiny* performance hit -- this is because *any*
".htaccess" files need to be parsed for each and every request
from the containing directory or any of its subdirectories.
Thanks, I am considering changing some stuff around in my site and
moving the php files (currently there is only one file) to a new
directory since they will all have the same structure. I am still
working out the details in my head.
 
K

kayodeok

Incidentally, that's a good thing, because it's a popular but
fatal misconfiguration of the server; leave AddType alone and
locate the AddHandler directive on the same page of the
documentation.

For more info, consult your old friend:

<http://www.google.com/search?q=406+http+accept+addtype>

Thanks, I will look into the AddHandler directive; actually I think
I ought to fully investigate these directives before playing around
with them...thanks for the pointer.
 
D

David Graham

ForceType application/x-httpd-php
</Files>

There is still a *tiny* performance hit -- this is because *any*
".htaccess" files need to be parsed for each and every request from the
containing directory or any of its subdirectories.
Hi Toby
I am thinking of using this because I just want my index page to be parsed
as php. I want the .html extension to remain on the index page because that
is what people are used to typing etc. so, to make sure I understand you,
what I do is put:

<Files index.html>
ForceType application/x-httpd-php
</Files>

exactly as above on 3 seperate lines in my .htaccess file, I assume it is
necessary for the .htaccess file to be in the same directory as the index
page. The extra hit due to the server reading this small text file would not
be that significant - or am i mistaken?

Alternative solution:
"To get around that, you may find it useful to do this configuration in the
main httpd.conf file, assuming you have permission."

I think I would mess things up good if I try your suggestion of altering the
httpd.conf file

Loads of questions follow (understand if you think a person with limited
knowledge would never be able to do this)

1. I would like to know how to get at this file
2. I have a remote host ISP. How do I know if I have permission to alter it?
3. Would I alter it by adding the 3 lines above?

thanks
David
 
T

Toby A Inkster

David said:
I am thinking of using this because I just want my index page to be parsed
as php. I want the .html extension to remain on the index page because that
is what people are used to typing etc. so, to make sure I understand you,
what I do is put:

<Files index.html>
ForceType application/x-httpd-php
</Files>

exactly as above on 3 seperate lines in my .htaccess file

Yes, that looks about right. For example, this PHP file:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/testfile.html

The source code is here:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/testfile.txt
I assume it is
necessary for the .htaccess file to be in the same directory as the index
page.

Not really, but it is probably easiest if it is. For example, mine is here:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/.htaccess

Although you can't see it (permission denied) so here is a copy:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/htaccess.txt

However, if I wanted to put it here:

http://www.goddamn.co.uk/tobyink/scratch/.htaccess

that would be OK! But I'd have to change the first line to:

<Files "sethandler/testfile.html">

I think so anyway. I've not tried it. Best to be safe and stick with the
same directory.
The extra hit due to the server reading this small text file would not
be that significant - or am i mistaken?

It is small, but as I say, the .htaccess file has to be processed for each
request, not only to (in my example) "testfile.html" but to any file in
the whole "sethandler/" directory, and any subdirectories that
"sethandler/" had.
1. I would like to know how to get at this file

It's usually kept somewhere in "/etc/httpd/conf/" on Linux and UNIX
servers.
2. I have a remote host ISP. How do I know if I have permission to
alter it?

By asking Tech support. If you have a cheap host or a shared server, you
probably won't be able to edit it. If you're on a dedicated host, you
might be able to. If you're on a shared host but you're paying them a lot
of money, they might make the changes for you so long as they are
reasonable and you ask nicely.
3. Would I alter it by adding the 3 lines above?

More or less, but it would probably have to be inside a bigger <Directory>
block:

<Directory "/path/to/the/directory/">
<Files index.html>
ForceType application/x-httpd-php
</Files>
</Directory>
 
T

Toby A Inkster

E

Eric B. Bednarz

Toby A Inkster said:
Eric B. Bednarz wrote:
"AddType application/x-httpd-php .html" works perfectly well (if a little
inefficiently).

The mailto protocol also 'works' perfectly well as a form handler.
That's why people keep using it.

You are depending on an accept header that either requests
application/x-httpd-php explicitly or has a */* fallback with a quality
value greater than zilch, not even speaking of problems related to
content negotiation.

Bednarz' /n/th law applies:

If you can accomplish a task with the same effort either the right way
and have it working all the time or the wrong way and have it working at
least for yourself, the explicit choice for the web must obviously
always be the latter.


And *now* you are allowed to call me silly, ole chap. :)
 
D

David Graham

Toby A Inkster said:
Yes, that looks about right. For example, this PHP file:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/testfile.html

The source code is here:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/testfile.txt


Not really, but it is probably easiest if it is. For example, mine is here:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/.htaccess

Although you can't see it (permission denied) so here is a copy:

http://www.goddamn.co.uk/tobyink/scratch/sethandler/htaccess.txt

However, if I wanted to put it here:

http://www.goddamn.co.uk/tobyink/scratch/.htaccess

that would be OK! But I'd have to change the first line to:

<Files "sethandler/testfile.html">

I think so anyway. I've not tried it. Best to be safe and stick with the
same directory.


It is small, but as I say, the .htaccess file has to be processed for each
request, not only to (in my example) "testfile.html" but to any file in
the whole "sethandler/" directory, and any subdirectories that
"sethandler/" had.


It's usually kept somewhere in "/etc/httpd/conf/" on Linux and UNIX
servers.


By asking Tech support. If you have a cheap host or a shared server, you
probably won't be able to edit it. If you're on a dedicated host, you
might be able to. If you're on a shared host but you're paying them a lot
of money, they might make the changes for you so long as they are
reasonable and you ask nicely.


More or less, but it would probably have to be inside a bigger <Directory>
block:

<Directory "/path/to/the/directory/">
<Files index.html>
ForceType application/x-httpd-php
</Files>
</Directory>
Thanks Toby - I understand most of that reply - not too sure what you mean
by bigger <Directory> block but I don't think I will be tinkering with
httpd.conf file anyway.
David
 
T

Toby A Inkster

Eric said:
You are depending on an accept header that either requests
application/x-httpd-php explicitly or has a */* fallback with a quality
value greater than zilch

But no I'm not.

============================== <quote> ==============================
[tai@ophelia (pts/1) ~]$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
GET /tobyink/scratch/sethandler/testfile.html HTTP/1.1
Host: localhost
Accept: text/html

HTTP/1.1 200 OK
Date: Mon, 24 Nov 2003 23:45:57 GMT
Server: Apache-AdvancedExtranetServer/2.0.47
X-Powered-By: PHP/4.3.3
Transfer-Encoding: chunked
Content-Type: text/html

working!


Connection closed by foreign host.
============================== </quote> =============================

You see? An Accept value exactly equal to "text/html". No fall backs,
nada, zilch.
And *now* you are allowed to call me silly, ole chap. :)

It seems cruel.
 
L

Leif K-Brooks

Eric said:
You are depending on an accept header that either requests
application/x-httpd-php explicitly or has a */* fallback with a quality
value greater than zilch, not even speaking of problems related to
content negotiation.

Have you actually tested that? I have a file in my home directory named
foo.oddext with this in it:

<?php
header('Content-Type: text/plain');
echo "Hello world.\n";
?>

And a .htaccess with this in it:

AddType application/x-httpd-php .oddext

So why does this do what it does?

leif@leif:~$ telnet localhost 8080
Trying 127.0.0.1...
Connected to leif.
Escape character is '^]'.
GET /~leif/foo.oddext HTTP/1.1
Host: localhost
Accept: text/plain
Connection: close

HTTP/1.1 200 OK
Date: Tue, 25 Nov 2003 02:16:31 GMT
Server: Apache/1.3.29 (Debian GNU/Linux) PHP/4.3.3
X-Powered-By: PHP/4.3.3
Connection: close
Transfer-Encoding: chunked
Content-Type: text/plain; charset=iso-8859-1

d
Hello world.

0

Connection closed by foreign host.
 
E

Eric B. Bednarz

Toby A Inkster said:
Eric B. Bednarz wrote:

But no I'm not.
[tai@ophelia (pts/1) ~]$ telnet localhost 80 [...]
Accept: text/html

HTTP/1.1 200 OK [...]
Content-Type: text/html

Yes, I see that I should have made my statement a little more verbose.
Add

... combined with a server(configuration) that returns content to the
client that the latter didn't advertise to be acceptable.

to the above.
An Accept value exactly equal to "text/html". No fall backs,
nada, zilch.

Sigh.

If you obmit the header value from the PHP script, you'll probably get
status 200 as well then, this time delivered as content-type
application/x-httpd-php. Try it. It may work for you.

You declare files with the extension html to be of the *type*
(*handler*). Why would you do that when in fact you wish to _deliver_
plain html to the client? The file will only be given to the handler
(and *then* return the desired output type) *if* the server decides that
this is an appropriate course of action in the first place (what is
appropriate under which circumstances is more than arguable; but
preferably not with me :).


To close the case, have a simple practical example:

Put bar.html and bar.xhtml in the directory foo.

Have MultiViews on and put

AddType application/x-httpd-php .html
AddType application/xhtml+xml .xhtml

in your configuration file.

Now visit /foo/bar with Mozilla and Opera 7.2.

Note what happens:

Mozilla gets xhtml; not surprising since it assigns the highest quality
value to that mime type.

Opera... gets xhtml as well. Not surprising either. It favours
text/html over application/xhtml+xml (q=0.9), but
application/x-httpd-php is catched all down the bottom of the ladder
with */*;q=0.1.

And that's how it is supposed to be. Because the server has been told
that bar.html *is* of a type with the lowest priority that can be
expected in the accept header of a web user agent, and apache cannot
know what the handler is supposed to do with it.


You must certainly continue to do things your way, you seem to do well
with it; but everybody else -- remembering that this is no private chat
-- should consider to

add *types* with the AddType directive
add *handlers* with the AddHandler directive

when configuring the server.

Because all of this anti-mnemonic geek jargon is not human readable for
the boy next door like me, that's why I write it down on the back of my
hands and never take a shower. :)


Cheers, Eric
 
T

Toby A Inkster

Eric said:
If you obmit the header value from the PHP script, you'll probably get
status 200 as well then, this time delivered as content-type
application/x-httpd-php. Try it. It may work for you.

The script doesn't have a "header value". The entire script consists of:

================== <quote> ==================
<?php
echo "working!\n";
?>
================== </quote> =================

as I have shown before.

The only *possible* problems can arise when the following conditions are
*all* met:

* the server is also set to use MultiViews (mine is for some
directories -- including this one);

* the browser requests the file without any extension; and

* the browser doesn't include */* or application/x-httpd-php in
its Accept header (most include the former).

Only when these three conditions are met do problems arise, and even then
the problem is only minor -- they'll be asked which version of the
document they want to view and only be given one option.

However, I think we can rule out the first two prerequisites in the case
of the OP -- he clearly doesn't have MultiViews enabled, or if he does,
isn't making use of them for links -- otherwise he wouldn't care so much
about giving a PHP file a ".html extension".
 
K

kayodeok

However, I think we can rule out the first two prerequisites in
the case of the OP -- he clearly doesn't have MultiViews
enabled, or if he does, isn't making use of them for links --
otherwise he wouldn't care so much about giving a PHP file a
".html extension".

My thanks to Eric and Toby for discussing this issue at length. I
have certainly learned a few tricks that should prove useful when I
need them.

This would be my first attempt at configuring the server either by
..htacess files or other means so I don't have Multi Views enabled
though I did consider the option because I came across a post by Toby
discussing this option when I was doing my research on Sunday.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top