The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> inserting some php code
shannonlp
post Dec 4 2006, 04:00 PM
Post #1


Novice
**

Group: Members
Posts: 29
Joined: 30-September 06
Member No.: 296



What is the best method to insert some php code into an xhtml document? I would like to insert the code rather than create a php document to render. Is this possible?

example use a regular xhtml document then insert a code snippet that pulls weather data based on ip address.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 4 2006, 04:55 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(shannonlp @ Dec 4 2006, 10:00 PM) *

What is the best method to insert some php code into an xhtml document?

You can basically insert it anywhere you like (with a few exceptions like HTTP headers). Then the server runs the PHP before the finished XHTML document is sent to the browser, so the latter will never see the PHP.

QUOTE
I would like to insert the code rather than create a php document to render. Is this possible?

You can indeed run PHP on a document with an .html extension, if that's what you mean. The document doesn't have to end in .php. This can be configured in e.g. a ".htaccess" file.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
JamieHarrop
post Dec 4 2006, 05:35 PM
Post #3


Advanced Member
****

Group: WDG Moderators
Posts: 129
Joined: 25-October 06
From: West Yorkshire, UK
Member No.: 570



QUOTE(Christian J)
This can be configured in e.g. a ".htaccess" file.


To do this, add the following code to your .htaccess file.

CODE
AddType application/x-httpd-php .html


To learn more about .htaccess see http://www.javascriptkit.com/howto/htaccess.shtml
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Dec 4 2006, 07:10 PM
Post #4


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



IIRC, using AddType like this can send browsers an error unless they include "*" (or "application/x-httpd-php") in their list of accepted types. AddHandler leaves the type as "text/html" which all browsers should accept.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 5 2006, 07:34 AM
Post #5


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Darin McGrew @ Dec 5 2006, 01:10 AM) *

IIRC, using AddType like this can send browsers an error

Indeed, http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addtype says
"The AddType directive maps the given filename extensions onto the specified content type"
and
"This mapping is added to any already in force, overriding any mappings that already exist for the same extension".
Does this mean that (in Jamie's example) a file with an .html extension will be sent with the content-type "application/x-httpd-php", overriding any existing "text/html" content-type? But when I tested this on some AddType sites with http://rexswain.com/httpview.html I still seem to get a "text/html" content-type and nothing else.

BTW, isn't AddType a peculiar way of making the server run PHP in e.g. .html files? Why (or how) does it work at all?

QUOTE
unless they include "*" (or "application/x-httpd-php") in their list of accepted types.

My browsers seem to accept "*/*". Which one(s) don't, if one wants to test?

QUOTE
AddHandler leaves the type as "text/html" which all browsers should accept.

I tried that in a .htaccess and it seems to work too:

CODE
AddHandler application/x-httpd-php .html

Is there anything else one should be aware of if using Addhandler for running PHP in .html files?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 7 2006, 10:01 AM
Post #6


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Christian J @ Dec 5 2006, 01:34 PM) *

Does this mean that (in Jamie's example) a file with an .html extension will be sent with the content-type "application/x-httpd-php", overriding any existing "text/html" content-type? But when I tested this on some AddType sites with http://rexswain.com/httpview.html I still seem to get a "text/html" content-type and nothing else.

Could someone please shed more light on this? mellow.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Curtis
post Dec 28 2006, 05:02 AM
Post #7


Newbie
*

Group: Members
Posts: 11
Joined: 28-December 06
From: California
Member No.: 1,397



I believe you're looking at it in the wrong way. The AddType directive doesn't affect the Accept HTTP header, it affects how the server handles requests based on file extensions. For example, if you were to map .html to be executable by PHP, then every request for a standard HTML page (with PHP or not) would pass through the PHP interpreter. It generally isn't a good idea for that reason, unless every page on your site already uses PHP.

What the browser sees is the content type returned by the PHP script.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 28 2006, 05:10 PM
Post #8


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



I did a test with this in the .htaccess:

CODE
AddType bar/baz .foo

this made the server send a bar/baz content type for .foo files. But when I tried this in the .htaccess:

CODE
AddType bar/baz .html

the previous text/html content-type wasn't changed for .html files.

QUOTE
What the browser sees is the content type returned by the PHP script.

Does this have higher priority than my .htaccess tests above? Or is it Apache that somehow doesn't let you override .html file content-types specifically?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Curtis
post Dec 29 2006, 08:50 AM
Post #9


Newbie
*

Group: Members
Posts: 11
Joined: 28-December 06
From: California
Member No.: 1,397



Hm, interesting. It might be due to settings in httpd.conf (main server config) or because of browser programming.

I'm not sure exactly what happens behind the scenes, but application/x-httpd-php is nowhere to be found in the response headers (.php or .html).

Also, AddHandler doesn't take MIME types, it takes handlers set by the SetHandler directive. Example:
CODE
AddHandler cgi-script .cgi .pl
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 27th April 2024 - 02:58 AM