Epiphany21 Code Snippets
Just another WordPress.com weblog

Viewing my source in color.

There was another snippet on SnippetLibrary.com that I didn’t particularly work as good as I had been hoping when playing with it. I edited, and changed it a little bit and now it works.

Let’s break the code down really quick.

if (isset($_GET['file'])) {
This code gets the file name. i.e. viewsource.php?file=index would show the index.php

$file = $_GET['file'];
Simply set up variable to save typing time later.

if ($file == 'config' || 'mysql') {
echo "Nice Try!";
} else {
show_source('./'.$file.'.php');
}

This pretty much does the work. First thing we are checking for are our config file and mysql files. Which have passwords in them we don’t want people to see. So, instead let’s give something alternative when viewing them. Like, a little teasing comment “Nice Try!”. Else, we will then show the source of the file.


if (isset($_GET['file'])) {
$file = $_GET['file'];
if ($file == 'config') {
echo "Nice Try!";
} else {
show_source('./'.$file.'.php');
echo $file;
}
} else {
echo "You did not specify a file name";
}
?>

You would add this link on any page of your site via a link that looks something like this.

$f = $file;
$link = "<a href='./viewsource.php?file=$f'>FILENAME</a>";

Or, statically
<a href="./viewsource.php?file=FILENAME">FILENAME</a>

Advertisement

One Response to “Viewing my source in color.”

  1. Also, please note – something along these lines should be used if you’re going to call the filename elsewhere.


    $file = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
    $piece = explode(".", $file);
    $file = $piece[0]; /* Get the part of the url before .php */


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.