Jump to content

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?
Use this:

http://64.40.144.139/squirrelmail/src/login.php
id: phptest@cannellent.com
password: phptest

This would be the url:
http://64.40.144.139/squirrelmail/src/redirect.php?login_username=phptest@cannellnet.com&secretkey=phptest&js_autodetect_results=SMPREF_JS_OFF&just_logged_in=1
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/redirect.php?login_username=phptest@cannellnet.com&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()
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.