Jump to content

php login to other site using curl


fix3r

Recommended Posts

I want to use php to login to another site and then go to that site using the screen name I was logged into and view a webpage on that site and submit a form and searching for text

 

mostly, i need help logging into that site through a php script.. how would I go about doing this

Link to comment
Share on other sites

<?php

$headers = array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0. Gecko/20061025 Firefox/1.5.0.8");

$url="THE WEBSITE";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=email&pass=pass");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$store = curl_exec ($ch);
curl_close ($ch);
print_r($store);

?>

 

Okay, I got the login to work. Now, how would I make it go to an existing page with the login information and then submit a whole new form? That I am a little confused about.

Link to comment
Share on other sites

Okay, sorry for spam. It seems that this code still outputs the html of the site. How do I make it not output the site at all?

 

$url="WEBSITE";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies1.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies1.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "ALL MY FIELDS");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$store = curl_exec ($ch);
curl_close ($ch);
//print($store);

Link to comment
Share on other sites

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.externalsite.co.uk/login.php");

curl_setopt ($ch, CURLOPT_POST, 1);

curl_setopt ($ch, CURLOPT_POSTFIELDS, "username=foo&password=bar");

curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$login = curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL, "http://www.externalsite.co.uk/memberspage.php");

$members = curl_exec ($ch);

curl_close ($ch);

?>

You now have a variable, $members, which contains the HTML output of memberspage.php after logging in with the username foo and password bar.

Hope that helps.. and works! 

Link to comment
Share on other sites

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.