Exoon Exoon - 7 months ago 19
PHP Question

Using PHP & Curl to login to my websites form

Im trying to login to my useraccount on a site i use to download files from so i can automatically grab the file without me having to visit the site.

This is the form:

<form method='post' action='/news.php'>
<div>
Username: <input class='tbox' type='text' name='username' size='15' value='' maxlength='20' />&nbsp;&nbsp;
Password: <input class='tbox' type='password' name='userpass' size='15' value='' maxlength='20' />&nbsp;&nbsp;
<input type='hidden' name='autologin' value='1' />
<input class='button' type='submit' name='userlogin' value='Login' />
</div>
</form>


Here is the PHP ive got so far.

<?php
$username="my_user";
$password="my_passs";
$url="the_url";
$cookie="cookie.txt";

$postdata = "username=".$username."&userpass=".$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);
$result = curl_exec ($ch);

echo $result;
curl_close($ch);
?>


Am i doing something wrong? It just displays the website at the moment but doesn't log me in. Ive never used Curl before.

Thanks

Answer

You should send via POST all data that the orignal form is sending. So you are missing autologin=1&userlogin=Login in your $postdata.

$postdata = "username=$username&userpass=$password&autologin=1&userlogin=Login";
Comments