Help - Search - Members - Calendar
Full Version: inserting some php code
HTMLHelp Forums > Programming > Server-side Scripting
shannonlp
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.
Christian J
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.
JamieHarrop
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
Darin McGrew
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.
Christian J
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?
Christian J
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
Curtis
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.
Christian J
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?
Curtis
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.