0

I want to make a multi registration form that posts on multiple sites the user doesn't need to type the same data over and over but he will post it on this form.The problem is that some sites may have captcha so I need a way to retrieve the captcha image on my form using cookies,sessions, curl I don't know which method can help me.

enter image description here

The problem has became complex because I need to store the session id and cookies because if i submit the form the other sites should know that is the same user that got requested the captcha. The php script that I used to get the catpcha is:

$ch = curl_init($url);//Site url
curl_setopt($ch, CURLOPT_COOKIEJAR, $file); //Save the cookie to a temporary file on the server
curl_setopt($ch, CURLOPT_COOKIEFILE, $file); 
curl_setopt($ch, CURLOPT_HEADER  ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$content = curl_exec($ch);

preg_match("'<div class=\"captchaimage\">(.*?)<a class=\"captcha-refresh-link hide\"'si", $content, $matches ); // getting the captcha image

echo $matches[1]; //Displaying the image

The first time when I use this code it shows a broken image:

enter image description here

When I refresh the page or I open in another tab the site that i request the captcha it shows the wanted captcha:

enter image description here

So I need some help because I haven't worked much with curl's and cookies and but I know the problem has a solution like this site https://www.betall.ie/register.html. I would appreciate some help :).

The php code is here: http://codepad.viper-7.com/FMKrGR

Antonio Papa
  • 1,596
  • 3
  • 20
  • 38
  • Not sure I understand. Do you want a user to signup to one site in your site collection and automatically be registered on all of them? If so you could just create the same entries in each database when the form is submitted. – GordonM Nov 07 '14 at 14:42
  • Yes I want to sign up to multiple sites automatically but some of the sites have a captcha and I cannot register the captcha value in the database you know what i mean? – Antonio Papa Nov 07 '14 at 15:08

2 Answers2

1

curl_setopt($ch, CURLOPT_COOKIEJAR, $file);

Can you use different cookie files for different $url's? I think that might help. The cookie items from different URLs will not mix.

Timothy Ha
  • 399
  • 3
  • 7
  • I don't understand your idea what should I actually do.Should I change the $file url in the CURLOPT_COOKIEJAR option? – Antonio Papa Nov 07 '14 at 15:16
  • @AntonioPapa Yes, use a separate cookie file for each website you are connecting to. – Timothy Ha Nov 07 '14 at 15:17
  • Different websites may use the same CAPTCHA libraries, so they may send similar variables in the cookies. If you use same file, values will get mixed. You can debug incoming values and check. – Timothy Ha Nov 07 '14 at 15:21
  • Now I'm currently testing on 1 site the multisite will be a next question :P. The problem is how to get the catpcha from the other site and when I submit the form send the right captcha using cookies and sessions. I am not getting the captcha image right now what should I do? – Antonio Papa Nov 07 '14 at 15:26
  • 1
    I see :-) I think you need to fully download the image, using cURL and passing CURLOPT_REFERER in the request for the image. – Timothy Ha Nov 07 '14 at 15:30
  • 1
    However, somebody succeeded with just passing cookies for the request of CAPTCHA file. http://stackoverflow.com/questions/2527729/how-to-retrieve-captcha-and-save-session-with-php-curl?rq=1 - please check the part where OP gives his updated code. – Timothy Ha Nov 07 '14 at 15:41
  • Still didn't solve the problem this is the code if you can find the solution http://codepad.viper-7.com/FMKrGR thank you – Antonio Papa Nov 07 '14 at 16:22
  • @AntonioPapa - after you find the matches, what do you do next? Do you construct the full URL of the CAPTCHA image and download it in another cURL request, passing the cookie? – Timothy Ha Nov 07 '14 at 16:51
  • No I don't download it in another cURL request I do exactly as the link that i sent you.I've tried downloading the image from another cURL request and showed SSL certificate problem: unable to get local issuer certificate – Antonio Papa Nov 07 '14 at 16:57
  • 1
    https? You are having a set of different problems all at once :-) a) multiple sites b) captcha c) cURL with https. Could this help? http://stackoverflow.com/questions/316099/cant-connect-to-https-site-using-curl-returns-0-length-content-instead-what-c – Timothy Ha Nov 07 '14 at 17:02
0

Found the solution for getting the captcha from another site example:

function getCaptcha($file){
   header ('Content-Type: image/png');
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
   curl_setopt($ch, CURLOPT_COOKIEJAR, $file);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   $out['result']  = curl_exec($ch);  //getting the html of the page 
   $out['error']   = curl_error($ch);
   $out['info']    = curl_getinfo($ch);
   curl_close($ch);  

   $matches = array();
   preg_match("'<div class=\"captchaimage\">(.*?)<a class=\"captcha-refresh-link hide\"'si", $out['result'], $matches );
   if(!isset($matches[1])){
      getCaptcha($file);
      return;
   }
   $img = $matches[1];
   $src = strpos($img, 'src="') + 5;
   $srcend = strpos($img, '"', $src);
   $img_src ='https://www.example.com' .  substr($img, $src, $srcend - $src);
   $img = substr($img, 0, $src)  . 'https://www.example.com' . substr($img, $src); //Getting the image captcha via regex

   $ch = curl_init($img_src);
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_COOKIEFILE, $file);
   $out2['result'] = curl_exec($ch);
   $out2['error']  = curl_error($ch);
   $out2['info']   = curl_getinfo($ch);
   curl_close($ch); //requesting the image via curl

   return $out2['result']; //returning the image
}
Antonio Papa
  • 1,596
  • 3
  • 20
  • 38