本文介绍下,用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 © 2013-<?php echo date('Y');?> by 程序员之家,欢迎您。 </p> </body> </html> Copy after login 调用示例: |