Getting source from protected site

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alon
Forum Newbie
Posts: 3
Joined: Thu Nov 30, 2006 7:33 am

Getting source from protected site

Post by alon »

Hello,

I’m trying to get a page source from a site.
I’m using a simple script :

Code: Select all

<?php
$handle = fopen("http://example.com ", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
  }
print ($contents);

fclose($handle);
?>

It works for the usual cases.

The problem I have is that I want to retrieve the information from a page that is on a site that I must enter my login credential first. Then I get only part of the page and not all the information as if I didn’t login.
I’m not trying to hack the page- it’s my information in it, but I want it copied to another place.

I tried adding the username and password to the fopen call :
fopen("http://username:password@example.com ", "rb");

But it didn’t work.

Any help would be appreciated.

Thanks,
Alon
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What kind of login is required to get in to these pages? Is it Basic HTTP Authentication?
alon
Forum Newbie
Posts: 3
Joined: Thu Nov 30, 2006 7:33 am

Post by alon »

I don't know. How can I tell ?

There is no pop up if this helps, only places to write down the user name and password.

Alon
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Maybe if you send POST headers you can get into the page...

Code: Select all

<?php
header("Content-Type: application/x-www-form-urlencoded");
//And...
$handle = fopen("http://example.com/?user=user&passwd=passwd", "rb"); 
?>
alon
Forum Newbie
Posts: 3
Joined: Thu Nov 30, 2006 7:33 am

Post by alon »

Thanks for your help.
I tried it but it didn't work.

Alon
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Using the CURL library is another option
Post Reply