Jump to content

fopen with login credentials


voxanBoxer

Recommended Posts

I am having a problem with fopen. I am opening a url and it works great BUT when I send login credentials, it doesn't work.

I thought it might be a redirect problem but I ran a test and opened a page that redirects 3 times in a row and it returned the 3rd page. So it isn't the redirect. The common problem is when I send login credentials it doesn't work.

Sometimes I get a page that says you must login first or sometimes it just returns blank.
Also, http vs. https doesn't change anything.

This is my code:

<?php
    $url = "https://www.myurl.com?id=Myname&password=mypassword";
    $fp = fopen( $url, 'r' ); 
    $content = "";

    while( !feof( $fp ) ) {
      $buffer = trim( fgets( $fp, 4096 ) );
      $content .= $buffer;
    }

echo $content;

?>
Link to comment
https://forums.phpfreaks.com/topic/33046-fopen-with-login-credentials/
Share on other sites

the manual says you can open a remote file with fopen: http://us2.php.net/fopen

without seeing the actual url you're trying to open, I'm sure no one can give you any definate help. How does the login work? Is it one of those browser popup things that asks for a password, or is it an online form? If it's a form, is it using the $_GET or $_POST method. Also, does the site send you a header redirect when you login?
curl was created for just such a task. You can get, post, or head -- you can even tell it to upload or download binary files, follow redirects, and I think it can even log into protected directories. It will take some effort, but this is a good place to start:

http://us3.php.net/curlexec
So far, no luck. One test returned the same page telling me to login first.

My test URL returns a "1":

<?php
    $url = "http://64.40.144.139/squirrelmail/src/[email protected]&secretkey=phptest&js_autodetect_results=SMPREF_JS_OFF&just_logged_in=1";
 
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

if (empty($buffer)) {
    print "Sorry, there was an error loading the page";
}else{
    print $buffer;
}

curl_close()
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.