Page 1 of 1

how to display the html source code of a page

Posted: Thu Oct 16, 2008 7:12 am
by swetha
how do i display the html source code of a website?i am planning to develop a tool : This is what i need to do.
i need to enter a website name.The output should be the HTML source code.
any method or function in PHP to do this?

thanks for any input

Re: how to display the html source code of a page

Posted: Thu Oct 16, 2008 7:19 am
by papa

Re: how to display the html source code of a page

Posted: Mon Oct 20, 2008 2:58 am
by swetha

Code: Select all

 
<script language="javascript">
function checkinput()
{
 alert ("inside javascript");
 if ((document.getElementById("sitename").value=="")){
 document.getElementById("pgcmptext2").value="Please Enter a domain name"; 
 document.getElementById("sitename").focus();
 alert ("invalid");
}
else
{      
  document.htmlviewform.submit();
  alert ("valid");
}
}
</script>   
                
                
<?php
$disp=0;
if (isset($_POST['sitename']))
{
$temp=$_POST['sitename'];
$handle = fopen($temp,"rb");
if ($handle==true)
{
   $disp=3;
   while (!feof($handle))
  {
      $buffer = fgets($handle, 4096);
      $result .= htmlentities($buffer);
  }
     fclose($handle);
}
else
{
    $result="Unable to access domain.Please verify address and try again";
}
}
?>  
<form method="post" action="" name="htmlviewform" >
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr> 
<td width="25%"><div class="textstylename1">Enter domain name:</div></td>
<td width="1%"></td>                
<td width="62%"><input name="sitename"  id="sitename" type="text" maxlength="300" size="50" value="<?php if (isset($_POST['sitename'])) echo ($_POST['sitename']) ?>"/></td>
<td width="12%"><input type="button" value="submit" name="subm" onclick="checkinput()"></td></tr><tr><td colspan="4">&nbsp;&nbsp;</td>
              </tr>
                <tr>
                <td colspan="4"><div align="center"><textarea id="pgcmptext2" readonly="readonly" name="htmltxt"><?php  echo $result;?></textarea></div></td>
<tr>
</table>
 </form>
 

this is the complete code which i have written for viewing the html source code of a form.this
works fine for blank values/valid values entered in the site name.

The problem is here :If i enter an invalid address
like
"http://www."
"http://www.abc.du" etc ,then the textarea displays this error message:
"Unable to access domain.Please verify address and try again"(which is fine).
The problem is that along with this i get the following error too.


"Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home2/king/engine-www/tools/html-code.php on line 247

Warning: fopen(http://www.abc.du) [function.fopen]: failed to open stream: No such file or directory in /home2/king/engine-www/tools/html-code.php on line 247"

note : Line 247 is "fopen".how do i rectify the above error.pls help.
once again how do i change fopen for invalid URL's

Re: how to display the html source code of a page

Posted: Mon Oct 20, 2008 11:20 am
by inet411

Code: Select all

<?PHP
$url = 'http://www.inet411.com/webmaster-tools/source-code-viewer/';
$contents = file_get_contents($url);
echo nl2br(htmlentities($contents));
?>
The nl2br is to convert the line breaks to <br /> for display purposes.
The htmlentities is also for display purposes.

I put a sample of the script in action at the url above.