Thursday, April 9, 2009

Creating Captcha using PHP and Handling Captcha using C# webbrowser 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.

Some websites are using some api (e.g recaptcha.net). This anti-bot service is free. Even they are providing this captcha content in the form of audio also.

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, strlen($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.

I have created the below ModalesMsgBox Class in C#. This class will display the Captcha image in separate form along with a textbox for getting the captcha text.
The entered text will be passed to the webbrowser control for allowing continuous execution of the bot/crawler/scrapping program.



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 need any software development and enhancement related work, please feel free to contact us for getting cost effective solution while keeping high quality and performance. Visit our website www.qualitypointtech.com


More Articles...

4 comments:

Unknown said...

Nice Stuff!

Commendable Blog indeed!

Great Going!

Dear Blogger, need your valuable feedback for:

www.octandigital.com

Regards,
Mehta

Rajamanickam Antonimuthu said...

Thanks for your comments.

Thanks,
Rajamanickam

Unknown said...

Thanks for this Captcha Code you just need to copy paste and its working...

Inyavic said...

Thanks for this tutorial on Creating Captcha using PHP and Handling Captcha using C# webbrowser control. Expecting more from you

Search This Blog