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

php中文网
发布: 2016-07-25 08:57:01
原创
2118人浏览过
本文介绍下,用php实现查看页面源代码的一个例子,用于显示或查看网页的源代码,有需要的朋友参考下吧。

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

代码:

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊
<?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速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号