<?php
$c=curl_init();
//password is empty, no login yet
if (empty($_POST['password'])) {
curl_setopt_array($c,array(
CURLOPT_URL => 'url',
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => TRUE,
CURLOPT_REFERER => 'referer'
));
$data=curl_exec($c);
//curl doesn't write cookies, so we do it ourself
preg_match('#PHPSESSID=([a-z0-9]+)#',$data,$sessid);
$sessid=$sessid[0];
$cookie=fopen('cookie.txt','w');
fwrite($cookie,$sessid);
fclose($cookie);
//replace the form-tag's action attribute with this proxy file
//might need preg_replace if the form-url isn't always the same
$data=str_replace('login.php', $_SERVER['SCRIPT_NAME'],$data);
//replace the captcha with our local image
$data=str_replace('original.captcha', 'captcha.png',$data);
//get captcha for this session and save it locally
//otherwise the shown captcha is not for
//this script but for our real browser session
//include our selfmade cookie-data
$f=fopen('captcha.png','w');
curl_setopt_array($c,array(
CURLOPT_URL =>'url/original.captcha',
CURLOPT_COOKIE => file_get_contents('cookie.txt'),
CURLOPT_HEADER => false,
CURLOPT_REFERER => 'referer'
));
$captcha=curl_exec($c);
curl_close($c);
fwrite($f,$captcha);
fclose($f);
//display the new form
echo $data;
}
else {
//generate data-string to post to form
$post_data=array();
foreach($_POST as $k=>$v) {
$post_data[]=$k.'='.$v;
}
$post_data=implode('&',$post_data);
//read our selfmade cookie to get the response for our script's session
$cookie=file_get_contents('cookie.txt');
curl_setopt_array($c,array(
CURLOPT_URL => 'form_url',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_COOKIE => $cookie,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HEADER => TRUE,
CURLOPT_REFERER => 'referer'
));
$data=curl_exec($c);
//several new cookies, get the values and save them
if (strstr($data,'Set-Cookie')) {
preg_match('#cookievalue=([a-z0-9]+)#',$data,$match);
$match=$match[0];
$f=fopen('cookie.txt','w');
fwrite($f,$cookie.'; '.$match.';');
fclose($f);
}
//we are logged in, now move on and get everything you want
//close connection in the end
curl_close($c);
}
?>