0

I want to login to a site automatically using curl. But the site is saying cookies must be enabled in your browser. I searched a lot but couldn't find it. If anyone could help,

After searching a lot in website help, I found out that the site uses two cookies Moodlesession and sessionid. I don't know how to handle them in curl. Please help.

The form is as follows:

<form action="http://site_name/login/index.php" method="post" id="login">
      <div class="loginform">
        <div class="form-label"><label for="username">Username</label></div>
        <div class="form-input">
          <input type="text" name="username" id="username" size="15" value="">
        </div>
        <div class="clearer"><!-- --></div>
        <div class="form-label"><label for="password">Password</label></div>
        <div class="form-input">
          <input type="password" name="password" id="password" size="15" value="">
          <input type="submit" value="Login">
          <input type="hidden" name="testcookies" value="1">
        </div>
        <div class="clearer"><!-- --></div>
      </div>
    </form>

The curl script I'm using is:

<?php
    $username="myusername";
    $password="mypassword";
    $url="login_url";
    $cookie="cookie.txt";
    $postdata="username=".$username."&password=".$password;
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    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_TIMEOUT, 60);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($ch, CURLOPT_REFERER, $url);

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    $result = curl_exec ($ch);

echo $result;
curl_close($ch);

?>

lonecoder
  • 65
  • 1
  • 7

2 Answers2

1

Add this option to cURL configuration:

curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');

And ensure you have a file called cookie.txt (for example write it to /tmp directory) and give permissions 777 (or make apache user the user of this file and give permissions 644)

I check this answer

Community
  • 1
  • 1
Sal00m
  • 2,938
  • 3
  • 22
  • 33
  • can you please tell me how to set permissions in windows platform? – lonecoder Oct 22 '14 at 13:46
  • I'm using ubuntu, but, afaik, is: Right click in the file, properties, and then in Security you can set the owner and the permissions. You can search in google about how to change permissions searching for "change file permission windows" – Sal00m Oct 22 '14 at 15:34
0

Perhaps you have to send in the postdata variable the input 'testcookies'. Something like:

$postdata="username=".$username."&password=".$password."&testcookies=1";
Serpes
  • 672
  • 4
  • 14