1

I am trying to login to a web page and then retrieve data from a page that is only visible to logged in members. I seem to now be able to log in using CURL, however I'm unable to retrieve the page that I want.

function login($url,$postData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, "http://domain.com/".$jobUrl."");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;

}   


$html=login('https://www.domain.com/users/ajaxonlogin.php',
'username=myemailaddress&passwd=mypassword');

echo $html;

I am able to get the following response:

{"status":"success","goto_url":"https:\/\/www.domain.com\/dashboard\/"}

I have tried adding to the bottom of the login function:

$url = "http://www.domain.com/page-that-i-want-but-requires-log-in.php";
$results_page = curl($url);
echo $results_page;

But I only receive the page as if I was NOT logged in...

Any pointers please?

Thanks.

Bruce

Dingo Bruce
  • 405
  • 1
  • 7
  • 14
  • You'll probably need to handle cookies to store the session id, that includes two things - the jar to store the cookies and sending the cookie on the next request. – user2182349 Jul 10 '15 at 01:20
  • The login system probably uses session variables, which relies on cookies. You need to save the cookies that the server sends back, and include them in future calls. See the `CURLOPT_COOKIEFILE` and `CURLOPT_COOKIEJAR` options. – Barmar Jul 10 '15 at 01:20
  • You are probably right... But I wouldn't know where to start with that either? How is that achieved with CURL? – Dingo Bruce Jul 10 '15 at 01:22
  • @Barmar That duplicate thread you linked to is quite different isn't it? The username and password variables are via a URL and not passed via an AJAX request. – Dingo Bruce Jul 10 '15 at 01:28
  • That difference is irrelevent. You just need to know how to save the session cookie. – Barmar Jul 10 '15 at 01:34
  • You shouldn't use `json_encode`. Just give the array directly to `CURLOPT_POSTFIELDS`. – Barmar Jul 10 '15 at 01:35
  • Using the example from the other thread all I receive is an eventual 301 Moved Permanently Error. – Dingo Bruce Jul 10 '15 at 01:46

0 Answers0