View Full Version : code to read a web page

08-30-2006, 06:07 PM
How can I read another web page in php?

I tried

$url = 'http://www.yahoo.com';
$contents = file_get_contents($url);
echo $contents;

but I get an error: The specified CGI application misbehaved by not returning a complete set of HTTP headers.

08-30-2006, 06:20 PM
$lines = file("http://www.google.com/index.html");
foreach ($lines as $line_num => $line)
print $line;

08-30-2006, 06:24 PM
I get the same error.

08-30-2006, 06:27 PM
i also tried on another unix server and got returned


08-30-2006, 06:27 PM
You can try this, but be a little careful about security, because you
can view the contents of not only .html, but .php code also.

echo '<form name="form1" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
echo '<input type="text" name="selected_file">';
echo '<input type="submit" value="View">';
echo '</form>';
echo '<hr>';
// security - checks for the right referrer
if(substr($_SERVER['HTTP_REFERER'],-strlen($_SERVER["PHP_SELF"])) == $_SERVER["PHP_SELF"])
$real_adress = 1;
$real_adress = 0;
if(!$_POST['selected_file'] == '' && $real_adress == 1)
$userfile = $_POST['selected_file'];
echo '<b>full path:</b> ' . $userfile;
$myArray = split('/', $userfile);
$selected_file_name = end($myArray);
echo '<br /><b>filename:</b> ' . $selected_file_name . '<hr>';
$strFile = file($selected_file_name);
$newStr = join(" ",$strFile);
echo '<div style="background:whitesmoke; width: 600px;">';
#HTML comment "start copy here"
echo "nnn<!--// -----n";
echo "----- -->nnn";
// HTML comment "END copy here"
echo "nnn<!--// -----n";
echo "END COPYn";
echo "----- -->nnn";
echo '</div>';
echo '<p><hr><i>End Of File</i></p>';
echo 'Please select a file.';

08-30-2006, 07:14 PM
That doesn't work on remote servers, does it?

I just tried it out and it seems like it doesn't work, but ... what are the risks here? Can people read my php code using this method?

08-30-2006, 07:26 PM
Your code is correct, are you running this on apache or iis?
Have you looked here? http://bugs.php.net/bug.php?id=25863

The code above will actually only read a file on your local webserver, it strips the address out... eg if you did http://www.icelolly.com/index.php but ran it on http://www.hazelryan.co.uk it would display the code for http://www.hazelryan.co.uk/index.php
I know of no php method that can read someone else's php code - because the php would be parsed before any output was sent.

08-30-2006, 08:11 PM
Fumigator ...

Oh, I didn't see your original part about accessing other URL's.
You're correct, it's for your own files, and yes, people can see
your PHP, so don't really use it without some security.

I know there's a Perl method to open HTML source from any site,
but haven't seen the PHP method yet (there must be a way).

If I find it, I'll post it.

08-30-2006, 08:20 PM
OK ... here it is:

Two methods (an array and a string) ...

// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.yahoo.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";

// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.yahoo.com/'));
echo "<br><br><br> Another Example <br><br>";
echo $html;