php 查看頁面原始碼的實作代碼(圖文)

WBOY
發布: 2016-07-25 08:57:01
原創
1836 人瀏覽過
本文介绍下,用php实现查看页面源代码的一个例子,用于显示或查看网页的源代码,有需要的朋友参考下吧。

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

代码:

<?php 
/**
* 显示与查看网页源代码  
* edit:bbs.it-home.org
*/
// 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 程序员之家,欢迎您。 
</p> 
</body> 
</html> 
登入後複製

调用示例: php 查看頁面原始碼的實作代碼(圖文)



來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!