Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    code to read a web page

    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.

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $lines = file("http://www.google.com/index.html");
    foreach ($lines as $line_num => $line)
    {
    print $line;
    }
    Last edited by MRMAN; 08-30-2006 at 05:24 PM.

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    I get the same error.

  • #4
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    i also tried on another unix server and got returned

    ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

    Code:
    <?php
      	  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;
     	  else
      	     $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 "START COPY HEREn";
      	       echo "----- -->nnn";
      	      highlight_string($newStr);
      	      // HTML comment "END copy here"
      	      echo "nnn<!--// -----n";
      	      echo "END COPYn";
      	      echo "----- -->nnn";
      	      echo '</div>';
      	      echo '<p><hr><i>End Of File</i></p>';
      	  }else{
     	      echo 'Please select a file.';
      	  }
     	 ?>

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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?

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Your code is correct, are you running this on apache or iis?
    Have you looked here? http://bugs.php.net/bug.php?id=25863

    @Fumigator
    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.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    OK ... here it is:

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

    Code:
    <?php
    // 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;
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •