PHP/CFML Source Viewer

What is PHP?

PHP is an open source, server-side, HTML embedded, scripting language used to create dynamic Web pages. In an HTML document, PHP code is embedded into the HTML source document and interpreted by the web server, which generates the HTML document. Because PHP is executed on the server, the client cannot view the PHP code. The extension of a PHP file is “.php”

What is CFML?

CFML is also a server-side scripting language. The output of a CFML is an HTML document. CFML can also be used to generate other languages, such as XML, JavaScript and CSS. Files created with CFML have the file extension “.cfm“. Because CFML is executed on the server, the client cannot view the CFML source code.

Can you view the PHP source code from a Remote Server?

NO. Since PHP code is executed on the server, and its HTML output is sent to the browser, the PHP source code will not show on the browser´s side. It will not even show in a code grabber, as grabbers are only capable to grab the HTML output created by the PHP page. When you visit a Web PHP page, the server interprets the PHP code, and generates an output in HTML. The PHP itself cannot be downloaded. Same thing applies to a CFML.

If you have administrative access to that server, then you can view the PHP source code. If you don’t have access, then NO.

Since the only way you can see the PHP source code is from your own domain (or other domains that you have access). Then why do you need a PHP/CFML Source Viewer?

The objective of the PHP/CFML Source Viewer is to show others your PHP (or CFML) source code.

Instructions

If you decided to use the code below, the only changes you need to do is to edit the correct file names(s) and file path(s). Copy the code below and paste it to a text file, and name it “sourceviewer.php”. Then load it with the associated external files, into your server.


<?php
/************ FILENAME: sourceviewer.php ************/
/*
* The $files array is the only change you need to do.
* Write the correct file names, and the correct paths.
* This file and the external files should be in the
* same domain .
* Separate files by comma.
*/
$files = array(
'math.php', //external PHP file
'separatedirectory/randomize.cfm', //external ColdFusion file
);
/***************** END OF EDITING *****************/

/*****************************************************
* For GET requests, print a list of links to the files
* and a form button to submit the file name. For POST
* requests, show the source of the submitted file.
******************************************************/

if( !$_POST ) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Source Code Viewer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div>

<h1> Output and Source</h1>
<ol>
<?php

// Loop through $files array, creating links to each file and its source code.
foreach( $files as $file ) {
?>
<li style="margin-bottom: 10px;">
<a href="./<?php echo $file . '">'. $file; ?></a><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="filename" value="<?php echo $file; ?>">
<input type="submit" value="View Source">
</form>
</li>
<?php
}
?>

</ol>
</div>
</body>
</html>
<?php
} else {
// Initialize error variable.
$error = false;

// Be sure we have something in the variable.
if( isset( $_POST['filename'] ) ) {

// Get and clean the file named in the $_POST array.
$filename = (ini_get('magic_quotes_gpc') ) ?
'./' . trim( strip_tags( stripslashes( $_POST['filename'] ) ) ) :
'./' . trim( strip_tags( $_POST['filename'] ) );

// Be sure the cleaned variable is not empty.
if( $filename ) {

// Show the source of the file.
if( @!show_source( $filename ) ) {
$error = true;
}
} else { $error = true; }
} else { $error = true; }

if( $error ) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Error</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div>
<h1>Error</h1>
<p>File does not exist.
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">GO BACK</a></p>
</div>
</body>
</html>
<?php
}
}
?>

Try the Source Viewer.

UPDATE: Another Simpler Way To View PHP Source Code

Some Web servers support the phps extension. If this is the case then you don´t need the Source Viewer code above. All what you need is change the file extension from php to phps. If you are lucky, your web server might be configured to display the source of files with a phps extension. This does not apply to ColdFusion files though.

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

One Response to “PHP/CFML Source Viewer”

  1. Kathy says:

    Thank you for sharing the source code. It is a great help.
    Kathy