Warning: include_once(/www/templates/gt/parts/blogs/carptips/first.php) [function.include-once]: failed to open stream: No such file or directory in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 19

Warning: include_once() [function.include]: Failed opening '/www/templates/gt/parts/blogs/carptips/first.php' for inclusion (include_path='.:/usr/local/lib/php') in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 19

Friday, June 10, 2005

Displaying feeds in non-PHP pages 

CaRP is most commonly used to integrate RSS feeds into PHP pages--that is, webpages whose name ends with ".php". Why does the filename extension matter, and how can newsfeeds be displayed on pages with different filename extensions?

The reason that the filename extension matter is that the webserver software uses the extension to decide whether and how to process the file before sending it to the web browser. If the extension is ".php", that tells the webserver that it may contain PHP code, and that that code should be executed before sending the data to the browser. If it ends with ".asp", that tells the webserver that the file may contain ASP code that needs to be executed. Similarly, an ".shtml" may contain SSI code that needs to be executed.

By only attempting to execute the kind of code that's indicated by the filename extension, the webserver can run faster, and security is increased because the likelihood of accidentally executing something that wasn't meant to be interpreted as executable code is minimized. Generally, files with the extensions ".html" and ".htm" do not contain any code for the server to execute (though they may contain JavaScript code to be executed by the web browser). Thus, ".html" and ".htm" can be server more efficiently than other kinds of files.

Once the server has finished any necessary code processing, the filename extension no longer matters, because in the end, the data that is sent to the web browser is always HTML (...at least in all the cases I'm discussing now. Obviously there are other cases like images, stylesheets, files containing only JavaScript code, etc.)

So let's say you want to display a feed in a file named "thenews.html". You could use JavaScript, but that wouldn't work for people who have JavaScript disabled in their browsers, and wouldn't help with search engine optimization, since search engines don't look at JavaScript. If "thenews.html" is a new file, you could just name it "thenews.php" instead. But if it's an existing file that's linked to from various other places, possibly including the search engines, what you'll want is a way to run PHP code in a ".html" file. If you're lucky, your web server is running Apache, and the server administrator hasn't disallowed the following. If either of those isn't the case, there may not be a solution available to you.

The way to get Apache to execute PHP code in ".html" files is to put something like the following into a file named ".htaccess", and to upload it to your webserver (or if you already have a .htaccess file, just add this line to it--don't just overwrite the old file, or other things may stop working!):

AddType application/x-httpd-php .html

Update: on some servers, that should be as follows instead:

AddHandler cgi-script cgi PHP html

The same applies to the code below too.

The .htacess file needs to go into your web directory either in the same folder where the ".html" file in question is located, or in a directory closer to the root. For example, if http://www.example.org/news/stories.html is located at /home/username/www/news/stories.html, then the .htaccess file could go in either /home/username/www/news or /home/username/www.

Note that the above line would tell Apache to processing PHP code in all .html files. If you're only going to add PHP code to one .html file, it's better to do this:

<Files stories.html>
AddType application/x-httpd-php .html

That way, only files named "stories.html" will have the extra processing applied to them.

Be aware that when PHP processing is turned on for a file, any part of the file that begines with "<?php", and in many cases ">?" is going to get treated as PHP code. If your file is in XHTML format and you have an XML prologue in it (eg. "<?xml version="1.0" encoding="ISO-8859-1"?>"), you may see a syntax error rather than a webpage. The solution to that problem is to use PHP to output the XML prologue, or at least part of it, like this:

<?php echo '<?xml version="1.0" encoding="ISO-8859-1"?>'; ?>

or this:

<?php echo '<'; ?>?xml version="1.0" encoding="ISO-8859-1"?>

One note in parting. If you want to display a feed in a ".shtml" page, you can do this easily by putting all of the PHP code into a separate file with a ".php" extension, for example, "stories.php", and then using SSI to incorporate it into the ".shtml" file like this:

<!--#include virtual="stories.php"-->

3/18/2006 Update

The SSI example above may need to be modified a little to work on your server. Here are two more things to try if the above doesn't work:

<!--#include virtual="/path/to/stories.php"-->

...where "/path/to/" is the path from your web root to the file--just "/" if it's in the web root directory itself.

<!--#exec cgi="/path/to/stories.php"-->
// posted by Antone Roundy @ 3:21 AM
(59) comments

This page is powered by Blogger. Isn't yours?

Warning: include_once(/www/templates/gt/parts/blogs/carptips/second.php) [function.include-once]: failed to open stream: No such file or directory in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 47

Warning: include_once() [function.include]: Failed opening '/www/templates/gt/parts/blogs/carptips/second.php' for inclusion (include_path='.:/usr/local/lib/php') in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 47

Warning: include_once(/www/templates/gt/parts/blogs/carptips/third.php) [function.include-once]: failed to open stream: No such file or directory in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 54

Warning: include_once() [function.include]: Failed opening '/www/templates/gt/parts/blogs/carptips/third.php' for inclusion (include_path='.:/usr/local/lib/php') in /www/gt/docs/carp/tips/2005_06_01_archive.php on line 54