Baumeister Mediasoft Engineering BME - Baumeister Mediasoft Engineering  PHP Application: Display/View Web Page Contents/Source - English Version PHP-Anwendung: Webseiteninhalt/-quellcode anzeigen - Deutsche Version
Baumeister Mediasoft Engineering
Bartels AutoEngineerCarLogServicesResourcesCompany ProfileContact Us
Baumeister Mediasoft Engineering » Resources » Web Development » PHP » Applications » View Web Page Source
PHP Applications

PHP Application: Display/View Web Page Contents/Source

PHP - PHP Hypertext Preprocessor

Introduction

Most web browsers provide a function for displaying the source code of the currently loaded web page. However, sometimes it is useful to access web page source code without having to load the web page into the web browser first. Examples might be web pages which immediately redirect to other web pages, style sheets (CSS files), JavaScripts (JS files), or web pages which could contain malicious code (you might want to check the source code first before allowing your web browser to load the web page and run or download any potentially malicious code). The file_get_contents() PHP function can be used to read an entire file into a string:

string file_get_contents(string filename)

A URL can be used as a filename parameter for the file_get_contents() function if the URL-aware fopen wrappers are enabled (PHP option/variable allow_url_fopen set to 1).

PHP Script

The following PHP and HTML code displays a HTML page with a form for entering the address (URL) of a file to be displayed. The form also provides a checkbox for wrapping long lines when displaying the source code. The content of the specified web address is displayed using a form/<textarea> tag. A Select All button is provided for selecting the displayed text in JavaScript-enabled web browsers.

<?php
// viewwebpagesource.php - Display/view contents/source of URI-specified web page
// Copyright (c) 2003-2006 by Manfred Baumeister (http://www.bme.ie)

// Page title
$pagetitle  'Baumeister Mediasoft Engineering :: Resources :: '
    
.'PHP Application: Display/View Web Page Contents/Source'
    
;
// Messages
$fmturl     '<p style="margin:0px;">"%s" contents/source:</p>'."\n";
$nosource   '<span style="color:red;">* empty / not found *</span>';
// Form parameters
$url        = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
$dowrap     = isset($_REQUEST['wrapsource']) && !empty($_REQUEST['wrapsource']) && ($_REQUEST['wrapsource'] == 'on') ? 0;
?>
<html>
<head>
<title><?php echo $pagetitle;?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body style="background-color:#cfcfcf;font-family:Arial;sans-serif;font-size:12px;">
<h3 style="font-size:13px;margin-bottom:0px;"><?php echo $pagetitle;?></h3>
<hr />
<form method="post" style="margin-top:4px;margin-bottom:4px;">
URL <input style="font-size:12px;" type="text" size="50"
 name="url" value="<?php echo empty($url) ? 'http://' $url;?>" />
<input style="font-size:12px;" type="checkbox"
 name="wrapsource"<?php if ($dowrap) { echo ' checked="checked"';}?> />Wrap
<input style="font-size:12px;" type="submit" value="Go" />
</form>
<?php
if (!empty($url))
{
    
// Start web page output
    
echo '<hr />'."\n";
    
// Display selected URL
    
echo sprintf($fmturl$url);
    
// Enable URL-aware fopen wrappers to allow for URL file reading
    
if ((double)phpversion() >= 4.2)
    {
        
ini_set('allow_url_fopen''1');
    }
    
// Read file
    
$s = @file_get_contents($url);
    if (empty(
$s))
    {
        
// Web page empty/access failure
        
echo $nosource;
    }
    else
    {
        
// Display web page contents/source using form/textarea
?>
<form name="_webpagesource_" style="margin-bottom:0px;">
<script type="text/javascript" language="JavaScript">
<!--
// Display select all button
document.write('<input style="font-size:12px;" type="button" value="Select All"'
    + ' onclick="document.forms[\'_webpagesource_\'][\'_src_\'].select();"'
    + ' />'
    );
//-->
</script>
<table width="100%" height="80%" border="0" cellspacing="0" cellpadding="0"><tr>
<td style="vertical-align:top;">
<textarea id="_src_" style="width:100%;height:100%;"
 wrap="<?php echo $dowrap 'virtual' 'off';?>">
<?php echo htmlspecialchars($s);?>
</textarea>
</td>
</tr></table>
</form>
<?php
    
}
}
?>
<hr />
<p style="margin:0px;font-size:9px;color:#666666;">
Copyright &#169; 2003-<?php echo date('Y');?>
 by <a href="http://www.bme.ie" target="_top">Manfred Baumeister</a>
</p>
</body>
</html>

Example

The following displays the HTML page from above using an <iframe> tag:

Click here to open the above page in its own window.

Baumeister Mediasoft Engineering » Resources » Web Development » PHP » Applications » View Web Page Source

PHP Application: Display/View Web Page Contents/Source • © 2019 Manfred Baumeister • Updated: 02 October 2010, 23:36 [UTC]

Baumeister Mediasoft Engineering, Clontarf, Dublin 3, D03 HA22, Ireland
© 2019 Manfred Baumeister

PHP Application: Display/View Web Page Contents/Source - English Version PHP-Anwendung: Webseiteninhalt/-quellcode anzeigen - Deutsche Version