Jump to content

PHP reading a webpage Source vs Content

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Frantic

Frantic

    Learning Programmer

  • Members
  • PipPipPip
  • 91 posts
When I use this code below:

[highlight="php"]
<?php

// Open a site and read the contents
$read = fopen("http://www.gmail.com";, "r")
or die("Couldn't open file");

// Read the bytes
$contents = fread($read,10000000);

// Display the contents
echo "$contents";
?>
[/highlight]

I see the actual page..... How do I see just the source though?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
It's been a while, but I believe $contents does contain the actual source code, its just when you echo it, the browser parses it.

Try a
echo htmlentities($contents);
If that doesn't work, you can always set the header to output text.

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You also seem to have an error on line 4:

[highlight="php"]
$read = fopen("http://www.gmail.com";, "r")
[/highlight]

See your extra ";" at the end of gmail.com?

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

It's been a while, but I believe $contents does contain the actual source code, its just when you echo it, the browser parses it.
Righto! Easy fix, echo "<pre>$contents</pre>";

#5
ETbyrne

ETbyrne

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
I didn't know that you could read PHP source code with other PHP scripts! Kind of scary actually because then there is no real way to hide MySQL passwords! :eek:

EDIT: I tried both of those methods and they just show the HTML.

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

ETbyrne said:

EDIT: I tried both of those methods and they just show the HTML.

Thats what its suppose to do.

#7
ETbyrne

ETbyrne

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Oh, ok I see. :)
My website > www.evanbot.com

#8
Guest_Jaan_*

Guest_Jaan_*
  • Guests
<?php

  // Open a site and read the contents
  $read = fopen("http://www.yoursite.xxx", "r") 
    or die("Couldn't open file");
    
  // Read the bytes
  $contents = fread($read,10000000);
  
  $contents = htmlentities($contents);

  // Display the contents
  echo "<pre>$contents</pre>";
?>

here's your full script.. works perfectly :)

#9
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

I didn't know that you could read PHP source code with other PHP scripts! Kind of scary actually because then there is no real way to hide MySQL passwords!

WOW! Thats something completely different. You will NEVER be able to read a PHP source from another script over HTTP! EVER!

#10
ETbyrne

ETbyrne

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Yeah the idea kind of scared me when I didn't read the first post correctly, but then I remembered how PHP works:

1. The user requests a page from the server.
2. If the page has PHP source code in it the server executes it.
3. The server sends the finished page to the user.

So the user never has a chance to see the source code.
My website > www.evanbot.com