Protected PHP source code display

For developer is very convenient to see PHP source code simply adding extension phps instead of php.
If you would like to access control to the source code, here is how to do it.
For example, everyone could see page www.example.com/page.php,
but only user joe will be able to see www.example.com/page.phps

cat /data/dir/htdocs/.htaccess
RewriteEngine on
RewriteRule (.*).phps$ /protected/sourcecode-phps.php [L]


cat /data/dir/htdocs/protected/.htaccess
AuthType BasicAuthName "Protected Access"
AuthUserFile /data/dir/htpasswd
require user joe


cat /data/dir/htdocs/protected/protected/sourcecode-phps.php
<?php
 $uri=$_SERVER['DOCUMENT_ROOT'].str_replace(
   ".phps",".php",$_SERVER['REQUEST_URI']);
  if (file_exists($uri))highlight_file($uri);
  else echo "PHP source file not found: $uri";
?>

This entry was posted in workday. Bookmark the permalink.

Leave a Reply