Problem handling cookies

I am working on a php script that can login to a webapp on a secure server and retrieve data via an export file.

I’ve managed to get the login to the server working (it’s using basic authentication which I’m adding to the url) and I arrive at the login page of the webapp. This requires me to POST the login details which I do. From what I’ve been able to determine so far by looking at the http is that:

  • from the login page there is a redirect to a cookie_test page
  • from here, the app should send a cookie
  • then it redirects to the ‘home’ page of the webapp

what happens for me instead is that when I send the post data I get bounced back to the login page with a message that my browser is not setup to handle cookies. I’m stuck on how to debug this further as I’m not sure if the problem lies with how I’ve issued the POST, managed the cookies, or if it is some mistake I’ve made in setting up the test environment (I’m using xampp to run the script which accesses the outside secure server)

[php]

    <?php
       $headers = array(
       "Authorization: Basic " . base64_encode("username:password"),
       "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
   "Accept-Language: en-us,en;q=0.5",
   "Accept-Encoding: gzip,deflate",
   "Connection: keep-alive",
       "Content-Type: application/x-www-form-urlencoded",
       );

       $ch = curl_init("https://url");
       $fp = fopen("page_info.txt", "w");
      
       curl_setopt($ch, CURLOPT_FILE, $fp);
       curl_setopt($ch, CURLOPT_HEADER, TRUE);
       curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch, CURLOPT_POST, TRUE); 
       curl_setopt($ch, CURLOPT_POSTFIELDS, "return=redirect_url&username=usrname&password=passwrd");  
       curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
       curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
       curl_setopt($ch, CURLOPT_FRESH_CONNECT, FALSE);
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

       $chData = curl_exec($ch); 
       if(!curl_errno($ch)){ 
          $info = curl_getinfo($ch); 
          echo 'Took ' . $info['request_header'] . ' seconds to send a request to ' . $info['url']; 
       } else { 
          echo 'Curl error: ' . curl_error($ch); 
       } 
       
       curl_close($ch);
       echo $chData;
       fclose($fp);
    ?>

[/php]

Any help on approaches I should be trying to debug this would be greatly appreciated.

have you checked to make sure you have cookies enabled on your computer? If not then enable them. If you do try enabling third party cookies. The error states that your computer is not accepting the cookies. try viewing the site from another computer and see if you get the same result.

have you checked to make sure you have cookies enabled on your computer? If not then enable them. If you do try enabling third party cookies. The error states that your computer is not accepting the cookies. try viewing the site from another computer and see if you get the same result.

When you say enable them on my computer are you referring to the browser? Should that be an issue when accessing sites via php scripts? Am I misunderstanding what you mean?

Incidentally I tried using curl from the command line to see if I could at least catch the cookie but I get a similar result.

yes i mean on your browser, the issue is with your browser not the scripts :wink:

As far as I can tell my browser has all cookie settings enabled. I tried both with Chromium and Firefox with the same results.

Going to the URL via a browser and logging in via the interface works as well, and I’ve seen the HTTP traffic that my browser accepts the cookie when I log in.

When I try to emulate the login using the script however, then the webapp is not issuing the cookie.

Sponsor our Newsletter | Privacy Policy | Terms of Service