Jump to content
xisto Community
coolcat50

Awesome Source Code Viewer Script

Recommended Posts

Hello! I have just came up with a sweet script to show the source code of any website and it only requires one file. This is the basis of the script and can be customized with CSS and other things and can be instituted as a public resource. Well I will provide the code and a step-by-step tutorial on each of its parts. This code has been tested by me. Enjoy!

<?php//This little tag starts our php script and is easily the most important part of the script.//We will start our base script here.//You can change some of the styles used here to your desired color.if ( $_GET['page'] == 'source' )//This line starts our source code page.//There are 2 'pages' to he script.//Both are defined with the get superglobal.{if ( isset ( $_POST['submit'] ) )//This line checks our input to make sure submit was clicked.{//We will define our variables here.$file=$_POST['file'];//This gives us our variable for the file.//Lets check to see if a file was entered.if ( !$file ){die("Sorry, you did not enter in a file to view.")}//Then we proceed//Here you could add a user verification var.//Such as a password or a verification number.//For now lets use a verification number$userverify=12345;//The user must enter this field in order to proceed.//Lets check and see if our user entered the verification number.if ( $_POST['verify'] == $userverify )//Checks verification{//Ok, lets get our script going.echo "<div style=\"background-color:yellow\">;//Ok that sets where the code will show.//Lets show our file urlecho "File URL:" . "$file" . "<br />";//There. Now lets use a rule to seperate the url and source.echo "<hr />";//Now lets show the user the source code.show_source($file);//There we go//Now lets close up this if statementecho "</div>";}//End user verify//If the user did not enter the verificationelse{echo "Sorry, you did not enter in the correct number. Try again."}//Ends error notification//Lets end our submit statement}//Now we need to set a statement that if the user did not submit anything.//An error pops up.else{die("Sorry, you shouldn't be viewing this page. Please go back.");}//Now we end our page statement}//now we start an else statement if they did not visit the source pageelse{?><!-- We end the php block for now and focus on html --><form action="url_source_viewer.php?page=source" method="post"><!-- There we set up our form and you must fill in the url statement with the appropriate url of the source page --><input type="text" name="file" value="File URL Here" /><!-- That defines our input form for the file --><br /><input type="text" name="verify" value="Verification Number" /><!-- There is where the user will enter the verification number --><br /><!-- Now lets display our verification number --><p>Verification Number: <?php echo $userverify; ?></p><br /><!-- There we echo out the number --><input type="submit name="submit" value="See Source" /></form><?php//We now need to end our else statement.}//And that is the code. If you copy and paste it please give me some credit.//This is a 'tutorial' that is supposed to teach and provide a resource.//Thank you?>

Live Demo:
Source Viewer Demo

There is my code. Also, feel free to move it to tutorials, I just felt that it should be under PHP programming.
Thank you for reading.
Edited by coolcat50 (see edit history)

Share this post


Link to post
Share on other sites

Looks great. That yellow background will wake them up...Thanks for the Demo and the script. Nice work.

Share this post


Link to post
Share on other sites

Here's a similar script only without syntax highlighting but line numbers and line highlight.

<?phpecho "<html><head><title>Source Code Demo</title><style type=\"text/css\">body {margin: 0;padding: 0;}td.auto{content: counter(number);counter-increment: number;padding: 5px;margin-right: 5px;background: #eee;color: black !important;}td {padding: 5px;font-size: 11px;font-family: Sans, Arial;}td.content:hover {background: #eee;}</style></head><body>\n";if ($_GET['page'] == "source" && isset($_POST['submit'])){$filename = $_POST['file'];if (!empty($filename) && $filename != ""){$handle = fopen($filename, "r");$contents = fread($handle, filesize($filename));fclose($handle);$contents = str_replace("<", "<", $contents);$contents = str_replace(">", ">", $contents);$contents = explode("\n", $contents);/*$contents = highlight_string($contents, true);$contents = explode("<br />", $contents);*/echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";foreach ($contents as $value){echo "<tr><td class=\"auto\"></td><td class=\"content\">".$value."</td></tr>\n";}echo "</table>";} else {die ("No file declared.");}} else {echo "<form method=\"post\" action=\"?page=source\"><input type=\"text\" name=\"file\" value=\"File URL Here\" onclick=\"this.value='';\" /><input type=\"submit\" name=\"submit\" value=\"View Source\" /></form>";}echo "</body></html>";?>

Syntax highlighting, as you can see, i commented out because it doesn't cover the entire syntax, since the SPAN element gets split up along the way. I also did not include any "verification number," for if it's going to be shown on the page, why include it?

Share this post


Link to post
Share on other sites

Sweet! How would you add line numbers to my script? Also, I know hwo to make it safer. Change the php.ini file to make the php colors be white and change HTML to a different color? Security reasons for that.

Edited by coolcat50 (see edit history)

Share this post


Link to post
Share on other sites

While I don't actually understand the php programming fully, I can see that this is indeed a VERY useful script. One quick question though, does the htaccess configuration of the page you are trying to view matter? I.e.: If you were trying to view a source of a page that would usually give you a 403 error (Forbidden, you do not have access, etc.) would you also be blocked from viewing the source through this script?

Share this post


Link to post
Share on other sites

I do not know exactly, but I do believe it will show because you aren't viewing the page but its source. Test it out and post here. One thing for sure it bypasses the Javascript anti-right click code.

Share this post


Link to post
Share on other sites

Sweet! How would you add line numbers to my script? Also, I know hwo to make it safer. Change the php.ini file to make the php colors be white and change HTML to a different color? Security reasons for that.

Similar to how i did to mine. In your code, you're outputting the file directly. You have to turn it into a string and add 'true' to the return parameter:
$source = show_source($file, true);
And in my script, pretend $contents is $source, and remove
$handle = fopen($filename, "r");$contents = fread($handle, filesize($filename));fclose($handle);

While I don't actually understand the php programming fully, I can see that this is indeed a VERY useful script. One quick question though, does the htaccess configuration of the page you are trying to view matter? I.e.: If you were trying to view a source of a page that would usually give you a 403 error (Forbidden, you do not have access, etc.) would you also be blocked from viewing the source through this script?

Viewing the source is dependant on your browser. If you run into a 403 forbidden error page and you view the source, you'll just see the HTML of the 403 forbidden error page.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.