0

I can login to a site with PHP cUrl, but I cannot navigate its pages. When I look at the cookie.txt file, login information appears, but when I want to access other pages, it seems like I'm not logged in. Why do you think it might? There are input data in cookie.txt, I show the relevant folder as the path, but I can't log in to other pages of the site.

   <?php


require 'curl.php';

$base = "https://siteadi.com/login";
$html_base = file_get_html($base);

$token  = $html_base->getElementByTagName("input[name=currentPageToken]")->getAttribute("value");
$agent  = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36";

$data = array(

    "username"     => "*******",
    "password"     => "*******"
);


//init curl
$ch = curl_init();



//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $base);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);


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

//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');


//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, true);


//execute the request (the login)
$store = curl_exec($ch);


//the login is now done and you can continue to get the
//protected content.

//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://siteadi.com/profil');



//execute the request
echo $content = curl_exec($ch);

curl_close($ch);

I can login, information comes to cookie.txt. However, when I switch to other pages of the site, it is perceived as if I am not logged in.

//The code block I tried is as follows

$ch = curl_init();
//the login is now done and you can continue to get the
//protected content.

//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'https://siteadi.com/profil');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);



//execute the request
echo $content = curl_exec($ch);

curl_close($ch);

  • By simply "re-using" your existing cURL handle and _only_ setting a new CURLOPT_URL, you will still be making a POST request to that new URL, and you will still be sending the same parameters in CURLOPT_POSTFIELDS. Which probably makes little sense ... much more likely, you need to make just a simple GET request for that second page. So use a new, fresh cURL handle instead ... – CBroe Mar 25 '22 at 09:40
  • Can you help by editing the codes? – Serkan Kuyu Mar 25 '22 at 09:51
  • Make an attempt yourself first. – CBroe Mar 25 '22 at 10:12
  • I tried but the problem persists. You mentioned a simple get operation, I tried it with cookie.txt, but it still doesn't see the session operation. – Serkan Kuyu Mar 25 '22 at 11:44
  • Edit your question and add what you tried, please. – CBroe Mar 25 '22 at 12:14
  • I edited now... – Serkan Kuyu Mar 25 '22 at 13:15
  • 1
    You are not providing any cookies with that request. `CURLOPT_COOKIEJAR` is the file cURL will store _received_ cookies in. To tell it where it should read cookies to _send_ with the current request from, you need to specify `CURLOPT_COOKIEFILE`. – CBroe Mar 25 '22 at 13:37
  • i tried but it didn't work – Serkan Kuyu Mar 25 '22 at 14:17
  • Are you sure your previous login request was even successful in actually logging you in to the site? I am wondering what the point of extracting that token from the original login form page is, if you then don't use that token value anywhere afterwards. – CBroe Mar 25 '22 at 14:33
  • 1
    @CBroe's comment about `CURLOPT_COOKIEFILE` is correct; without this, curl will not send previously stored cookies from `CURLOPT_COOKIEJAR`. Both are needed. See [this answer](https://stackoverflow.com/a/10307956/892493) for several curl login examples that use cookies and make subsequent requests for pages when "logged in". – drew010 Mar 25 '22 at 15:58

0 Answers0