views:

7569

answers:

8

Please, I am solving this problem.. I need to login into Facebook or Twitter or any other website from my PHP script running on my server. I am normaly doing that with CURL, saving cookies to some predefined file. But now I need something new.. I need to stay logged in with my browser, even when the script ends the login process. Is that something simple, I can't see.. or am I going into complicated territory? Something tells me, I would need to use javascript to set all cookies and sending the login data form?

If anyone has done loging into Facebook or Twitter with javascript, can you share some tips or the complete script with me, please?

Thanks for any tips and explaining me the overall logic.

A: 

Now there is the Facebook API available, perhaps this is the best port of call? I'm not sure about how to carry this out for twitter though.

I'm not too up to speed on this area, but I'd maybe have a look through This Link.

It may be of help. If not, have a dig around the facebook developers site

Hope this offers some sort of help, better than nothing I suppose...

Dave

Dave
A: 

I don't think this is supposed to be possible. When your PHP script logs in, it gets an authentication token/cookie for Facebook. That cookie is private and not supposed to be used on any other machine. There are hackish ways to do it, but none I can recommend.

Matthew Flaschen
+1  A: 

I would look into Facebook Connect, More info in this.

Phill Pafford
Thank you all very much for your answers.. but I need in general this functionality not only for Facebook, but also for Twitter and many other login forms in general. I would need to fake real browser login with my PHP or javascript script and let the user logged in after the script finishes. Thank you
neon
A: 

For your next job :) After a little surfing I found a script you (and sometimes me) needed.

/*
* Login to facebook
* $login_email : Account to login with
* $login_pass : Account password
*
* Returns true if logged in successfully, false otherwise
* Echoes any login error code
*
* Matt Smith - geekalicio.us
* Apr 23, 2009
*/
function fb_login($login_email, $login_pass){

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?login_attempt=1');
 curl_setopt($ch, CURLOPT_POSTFIELDS,'charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&locale=en_US&email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&pass_placeholder=&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84');
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 GTB5");
 curl_exec($ch);

 $err = 0;
 $err = curl_errno($ch);
 curl_close($ch);

 if ($err != 0){
 echo 'error='.$err."\n";
 return(false);
 } else {
 return(true);
 }

}

and then you can load home page with

if (fb_login($login_email,$login_pass)){
$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?login_attempt=1');
 curl_setopt($ch, CURLOPT_POSTFIELDS,'charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&locale=en_US&email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&pass_placeholder=&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84');
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_COOKIEJAR, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_COOKIEFILE, str_replace('\\','/',dirname(__FILE__)).'/fb_cookies.txt');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 GTB5");
$html = curl_exec($ch);

 curl_close($ch);

  echo $html;
}

Whole script I'm using located at http://pastie.org/619912 .
And yes, use it for good, not for evil :)

Mushex Antaranian
A: 

i am found an error on login as "Cookies Required

Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing."

dilip
A: 

how can check facebook username availability using remote PHP script ?

free seo tools
A: 

Hi, i try Mushex Antaranian's script and I can login to facebook but I would like to know how can I get status from my profile ?

pam