Enable PHP Syntax Highlighting on IIS 7

This post describes how to configure IIS 7 to output syntax highlighted source code for PHP files stored on the web server or site. This feature may be useful for development environments when you want to quickly make the source code accessible to other team members.

WARNING: Never attempt to do what is described below on any internet accessible web site! Doing so will greatly compromise the security of your web application.

The HTTP requests for the PHP source code are usually identified by the “.phps” file extension, e.g. http://localhost/index.phps. You will need to configure IIS to understand and handle the HTTP requests with this extension. There are two options for that: to create an IIS handler mapping for “*.phps” or to use the IIS URL Rewrite Module.

Configuring IIS Handler Mapping

PHP executable supports a command line argument “-s” which is used to display color syntax highlighted source code of any PHP script. You can create a handler mapping for “*.phps” and use “C:\[path to PHP installation]\php-cgi.exe|-s” as the executable, e.g.:

Handler Mapping for PHPS extension

After that you will need to rename or copy the .php file to .phps file and then request it from a web browser.

Using IIS URL Rewrite Module

If you do not want to rename each .php file to .phps, you can use IIS URL Rewrite rule and a simple PHP script (as suggested by Pierre Joye). In the root directory of your web site create a file called highlighter.php and put the following code inside:

<?php
if ( isset( $_GET['file'] ) ) {
  $file = filter_input( INPUT_GET, 'file', FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_HIGH );
  highlight_file( $file );
}
?>

After that add the following rewrite rule to the web.config file located at the root directory of your web site:

<rewrite>
<rules>
  <rule name="PHP Source Code" stopProcessing="true">
   <match url="(.*\.php)s$" />
   <conditions>
    <add input="{DOCUMENT_ROOT}\{R:1}" matchType="IsFile" />
   </conditions>
   <action type="Rewrite" url="highlighter.php?file={R:1}" appendQueryString="false" />
  </rule>
</rules>
</rewrite>

Now, if you request any url that has an extension .phps the highlighted source code will be returned in the response, provided that the corresponding .php file exists under the web site’s root directory.

Result of requesting a PHPS file

7,828 views

ruslany on October 6th 2009 in PHP, URLRewrite

PoorFairAverageGoodExcellent (5 votes, average: 3.40 out of 5)

2 Responses to “Enable PHP Syntax Highlighting on IIS 7”

  1. Gravatar ImageTweets that mention Enable PHP Syntax Highlighting on IIS 7 | RuslanY Blog -- Topsy.com responded on 06 Oct 2009 at 11:33 pm #

    [...] This post was mentioned on Twitter by Ruslan Yakushev. Ruslan Yakushev said: New blog post: Enable #PHP Syntax Highlighting on IIS 7 – http://tinyurl.com/y96kqoq [...]

  2. Gravatar ImageDotNetShoutout responded on 07 Oct 2009 at 3:05 am #

    Enable PHP Syntax Highlighting on IIS 7 – Ruslan…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles