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.
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:
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"?>'; ?>
<?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:
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:
...where "/path/to/" is the path from your web root to the file--just "/" if it's in the web root directory itself.
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