Real Web Page Source Code
When you use a browser's "View Source" menu item (generally also available with a right-click), then you get the source code of the web page.
But which source code? Here are 3 possibilities.
-
The browser won't give you the raw or original source code if it includes any PHP code. To get the source code with PHP code intact, you need to have access to the server to download the file (or have the owner of the web site give the file to you).
-
The browser might give you the actual source code it receives from the server. This would be the web page with PHP already having run and made any changes to the page that it has been told to do. It depends how the browser handles JavaScript-inserted content.
-
The browser might present the source code that results after certain JavaScript has run.
The below script gets the actual web page source code after PHP runs but before JavaScript runs.
<?php $URL = urldecode($_SERVER['QUERY_STRING']); echo '<pre style="overflow:auto; white-space:pre-wrap; outline:1px solid black; padding:.5em;">'; echo htmlspecialchars(file_get_contents($URL)); echo '</pre>'; ?>
No customization is necessary.
Save the file as sourcecode.php
or other *.php
file name. Upload the script to your server and make a note of its URL.
To use, type the script's URL into your browser, append a ?
character followed by the URL of the web page you want to see the source code of. Load that page and you will see the web page's real source code.
Here is an example of what you might type into your browser's address bar. (The example assumes the script is installed at https://domain.com/sourcecode.php
)
https://domain.com/sourcecode.php?https://spamfreeform.com/
The result would be the source code of the index page at the spamfreeform.com domain.
The short sourcecode.php
PHP script presents the real thing, the real source code exactly like it would be delivered to a browser.
(This content first appeared in Possibilities newsletter.)
Will Bontrager