Monday, March 9, 2009

Automatic login script

This script is to automated our log-in to some site.
You must have cURL installed to use this script.
When I started to code this script , at that time I was having some difficulties to code and finally I got the success to programmed auto log-in script. From internet I have research allot to create such a script , but there is no correct solution available for this script on internet . This script is really help you to logging in to different site automatically when you run it .

// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.external-site.com/Members/Login.php');

//Reference URL that where you going to login
curl_setopt($ch, CURLOPT_REFERER, "RefUrl");

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldname2=fieldvalue2,...');

//Cookie file
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

//set session cookie
curl_setopt ($ch, CURLOPT_COOKIE, 'cookie.txt');

//Usage of CURLOPT_FOLLOWLOCATION is important when sites use URL redirectors for //file downloads
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);


// Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
// not to print out the results of its query.
//Instead, it will return the results as a string return value
// from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$result = curl_exec($ch);

// CLOSE CURL
curl_close ($ch);

1 comment:

Ihsanullah said...

I read your article and will test Inshallah.