Creating Captcha using PHP and Handling Captcha using C# web browser control


These days captcha is used widely in most of the websites for preventing automated entry of details in their websites.

For those who are hearing the word Captch first time - You might have seen an image with blurred alpha numeric content with different font style and font size and with different orientation. It is used in the forms of websites to prevent automated entry of details using software programs such as bots and crawlers. So only the human can read those contents and we need to enter that content in the text box provided near this image. This system is called as Captcha.

If you want to avoid dependency of this third-party service you can create your own captcha images dynamically using GDLibrary of PHP. Simple Google search will give you the code. Please find below the sample one.


class CaptchaImages {

var $font = 'monofont.ttf';

function generateCode($characters) {
/* list all possible characters, similar looking characters and
                                          vowels have been removed */
$possible = '23456789bcdfghjkmnpqrstvwxyz';
$code = '';
$i = 0;
while ($i < $characters) { $code .= substr($possible, mt_rand(0,
                                             len($possible)-1), 1);    
      $i++;   
}   return $code;  
}   function CaptchaImages($width='120',$height='40',$characters='6') 
{
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new
                                                    GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i<($width*$height)/3; $i++ ) {
  imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 
      1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i<($width*$height)/150; $i++ ){imageline($image, mt_rand
                                        (0,$width), mt_rand(0,$height), 
 mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die
                     (               'Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color,
      $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['security_code'] = $code;
}
}
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters']
                                 > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaImages($width,$height,$characters);

Till now we have seen how to implement Captcha to avoid automated form filling using bots.

Sometimes we may need to navigate the websites using bots created using webbrowser control. As the bots can not read the captcha content, it is not possible to navigate the sites without interruption. In this case, we can create a code atleast for allowing the user to manually enter the captcha text while the bot continues the execution.


public partial class ModalesMsgBox : Form
{
public string captchaWord;
public string captchaurl;
public bool isEntered;
public ModalesMsgBox(string strcaptchaurl,string strMsg)
{
captchaurl = strcaptchaurl;
isEntered = false;
InitializeComponent();
lblMsg.Text = strMsg;

}

private void btnsubmit_Click(object sender, EventArgs e)
{
captchaWord = txtCaptcha.Text;
isEntered = true;
this.Close();
}



private void ModalesMsgBox_Load(object sender, EventArgs e)
{

try
{

webBrowser1.Navigate(captchaurl);

}
catch(Exception ex)
{

}

}

Find below the sample code for using the above class in the typical webbrowser control navigation code.


 string strMsgrd = "Please enter captcha letters and click 
'Enter' in this Dialog box.\n Don't type 'Enter'  and don't click 'Submit' button in the browser control";
string strCaptchImgrd = webBrowser1.Document.GetElementById
                                              ("capimage").GetAttribute("src");
ModalesMsgBox msgrd = new ModalesMsgBox(strCaptchImgrd, strMsgrd);
wait(70000);
msgrd.Show();
wait(500000);
while (!msgrd.isEntered)
{
Application.DoEvents();
}
string strCaptchard = msgrd.captchaWord.ToString();
webBrowser1.Document.GetElementById("captcha").Focus();
wait(70000);
webBrowser1.Document.GetElementById("captcha").InnerText = strCaptchard;
wait(700000);
HtmlElementCollection SubmitButton = webBrowser1.Document.
                                                  GetElementsByTagName("button");
SubmitButton[0].InvokeMember("click");

If you have better solution, just tell me !

1 comments:

Unknown said...

Excellent source about Captcha Code