php 查看页面源代码的实现代码(图文)

发布时间:2019-10-14编辑:脚本学堂
本文介绍下,用php实现查看页面源代码的一个例子,用于显示或查看网页的源代码,有需要的朋友参考下吧。

本节分享的这段php代码,可用于显示与查看网页的源代码。

代码:

<?php 
/**
* 显示与查看网页源代码  
* edit:www.jb200.com
*/
// 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') ? 1 : 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; 2013-<?php echo date('Y');?> 
 by <a href="http://www.jb200.com" target="_top">脚本学堂,欢迎您。</a> 
</p> 
</body> 
</html> 

调用示例:
查看页在源码