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);
?>