August 16, 2006

phps handlers

Cool trick to avoid making duplicates or symlinks to .php files in order for people to view your crappy php source. Also nice when your webhosting doesn't provide a handler for .phps. Go go more .htaccess fu and php'ness.

In summary place the following in a .php file somewhere, like viewsource.php or phpsrc.php:
<?php

  $file = preg_replace("/\\.phps$\", ".php", $_SERVER[\'PATH_TRANSLATED\']);
  $file = realpath($file);
  if (!file_exists($file)) {
   print("Error: No such file!!");
  } else {
   highlight_file($file);
  }

?>
Then add the following 2 lines to .htaccess:
AddType php-source .phps
Action php-source /viewsource.php
replace viewsource.php with your file name of choice, and viola!

For example, see the AAWC Formatter and its source.

Cheers,
Steve