Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibrarySnooping (Information Retrieval)

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

View Redirect Page Source Code

The software introduced and provided free with this article presents all the data received from the server when it responds to a request for a web page. What you get is the code received from the initial request — not how it might afterward be changed with Ajax calls or otherwise.

This project began as a tool for myself.

Here is what happened.

We published a tweet announcing the latest Possibilities article to be found online, as we virtually always do.

Someone who followed the link in the tweet also used a feedback form that provided the referring URL: https://t.co/UrWzbvp1cM?amp=1

Putting that URL into the Follow-Redirects Utility failed to provide a redirect trail.

My thought was that Twitter does the redirect with JavaScript. (The redirects utility works only with server response codes.)

Because the browser is redirected before the source code can be viewed, I assigned a project to myself — create software that appears to be a real browser but that doesn't react to anything the server sends. The purpose was to view the web page information, including headers, that the server responds with when the page is requested.

It would provide information not only about redirect pages but any public page.

Oftentimes, when I assign a project to myself, the end result has more features than originally envisioned.

It happened with this project, too. In addition to headers and web page source code, it also provides response code, header size, content size, and other information related to the process of requesting the page and the server's response.

It has a simple name: Web Page Source Code Viewer

When I gave Web Page Source Code Viewer the https://t.co/UrWzbvp1cM?amp=1 URL, I found out that the page does indeed redirect with JavaScript. It includes meta redirect code to redirect non-JavaScript browsers.

Screenshot and Example

This is a screenshot of the opening screen.

screenshot

For the example, we'll assume you typed in the URL of this article.

https://www.willmaster.com/library/snooping/view-redirect-page-source-code.php

Below is what you'll see in Web Page Source Code Viewer for the above URL. The data is in an iframe.

At the top of the page is an introduction, then the header lines and the source code of the web page.

The header lines can be handy. As an example, if you are a developer and need to quickly know what type of server an inquirer's website is running on, the header lines will tell you.

The source code of the webpage itself begins immediately below the first blank line.

Below the page source code, you'll see a table with information obtained during the web page request and the server's response. This is information provided by the PHP cURL class. The types of information may vary.

You will always get the URL of the page, the content type, and the server response code — if you actually reached an existing domain. (The server response code is labeled as "http_code".)

You will almost always get header and content sizes and time taken for connections, requests, and responses. And other measurements and information.

The information is available in case you find it necessary to have.

The Web Page Source Code Viewer Software

Below is the source code for the Web Page Source Code Viewer software.

It's a PHP script much smaller than originally anticipated. The total number of lines is just a twitch over a hundred.

Copy the source code and paste it into a plain text file. Save it as getsource.php or another .php file name that suits you. Upload it to your server and make a note of its URL.

No customization is required. Just type it's URL into your browser and use it.

<?php
/*
Web Page Source Code Viewer
Version 1.0
July 27, 2019

Will Bontrager Software LLC
https://www.willmaster.com/
*/
$HasURL = ( isset($_POST['url']) and strpos($_POST['url'],'http')===0 ) ? true : false;
function ProcessURL()
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,  // Return web page
        CURLOPT_HEADER         => true,  // Return headers
        CURLOPT_CONNECTTIMEOUT => 120,   // Timeout on connect
        CURLOPT_TIMEOUT        => 120,   // Timeout on response
        CURLOPT_FOLLOWLOCATION => false, // Do not follow redirects
        CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'],
        CURLOPT_REFERER        => ( $_SERVER['REQUEST_SCHEME']=='https' ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
        CURLOPT_VERBOSE        => true
    );
    $ch = curl_init($_POST['url']);
    curl_setopt_array($ch,$options);
    $content = curl_exec($ch);
    $err = curl_errno($ch);
    $errmsg = curl_error($ch) ;
    $info = curl_getinfo($ch);
    curl_close($ch);
    $info['errno']   = $err;
    $info['errmsg']  = $errmsg;
    echo <<<CONTENT
</div>
<h3 style="display:table; margin-right:auto; margin-left:auto;">URL Grabbed: <span class="nowrap">{$_POST['url']}</span></h3>
<div class="code">
<h2>Page Source Code</h2>
<p>
Below are both the header lines as returned by the server and the source code of the web page at <span class="nowrap">{$_POST['url']}</span>
</p>
<p style="margin-bottom:0; display:table; border:1px solid #ccc; border-radius:.25em; padding:.25em;">
The first blank line divides the header lines from the web page source code.
</p>
<pre style="margin-top:0;">
CONTENT;
    echo htmlspecialchars($content);
    echo <<<CONTENT
</pre>
</div>
<div class="content">
<table border="1" cellpadding="6" cellspacing="0" style="width:100%;">
<tr>
<th colspan="2">Information about the request and response</th>
</tr>
<tr>
<th>Label</th><th style="width:100%;">Info</th>
</tr>
CONTENT;
    foreach( $info as $k => $v )
    {
        echo <<<CONTENT
<tr>
<td>$k</td><td style="width:100%;">$v</td>
</tr>
CONTENT;
    }
    echo <<<CONTENT
</table>
CONTENT;
} # function ProcessURL()
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Page Source Code</title>
<style type="text/css">
* { box-sizing:border-box; }
html, body {  font-size:100%; font-family:sans-serif; }
.code { display:table; margin:2em auto; border:1px solid black; padding:1em; border-radius:.5em; }
.content { max-width:550px; margin:2em auto; }
table { border-collapse:collapse; }
th { font-size:90%; vertical-align:bottom; }
td { vertical-align:top; }
div > :first-child { margin-top:0; }
div > :last-child { margin-bottom:0; }
.nowrap { white-space:nowrap; }
input { width:100%; font-size:1em; }
input[type="text"] { border:1px solid #ccc; padding:.3em; border-radius:.3em; }
</style>
<body><div class="content">
<h1 style="text-align:center;">Get Web Page Source Code</h1>
<?php
if( $HasURL )
{
    ProcessURL();
    echo '<h4 style="margin-top:3em;">Test another URL?</h4>';
}
?>
<form method="post" enctype="multipart/form-data" action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF'])); ?>">
<p>
URL: 
<input type="text" name="url">
</p>
<p>
<input type="submit" value="Grab Source Code at URL">
</p>
</form>
<p style="margin-top:2em;">
Copyright 2019 <a href="https://www.willmaster.com/">Will Bontrager Software LLC</a>
</p>
</div>
</body>
</html>

With the above script on your server, you can check the source code of any public web page URL — or scripts or other files so long as http:// or https:// begins the URL.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC