简体   繁体   中英

CURL successful login redirecting to login page again

I am trying to get a value of some fields from my other domain. The other domain site has a login page. I am doing successful login in browser and the calling the file_get_contents() function but still it is returning me login page HTML.

I want HTML of the page which I am passing in URL.

Here is my code after adding cookie file

$COOKIEFILE = dirname(__FILE__) . '/cookie.txt';
$formFields['username']  = 'XXXXXX';
$formFields['password']  = 'XXXXXX';
$ch = curl_init();

// set some options on the handle
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL, 'https://www.mydomain.lcl/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formFields));
$data = curl_exec($ch);

Second Request after login

curl_setopt($ch, CURLOPT_URL, 'https://www.mydomain.lcl/dashboard');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
$result = curl_exec($ch);

Can anyone plz suggest me how to bypass login page and access the HTML of a page.

I am doing successful login in browser

… so now the browser can access the page behind the login.

and the calling the file_get_contents()

Your PHP program is not the browser. It doesn't have access to the page behind the login.

You need to login using an HTTP request you make from PHP (and then store whatever the login system expects you to send back with each request … probably a cookie).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM