Captcha Generator

This tutorial we will create a Capthcha Generator. And this simple project would be very helpful for your programming projects. Captcha Generator or a full form of CAPTCHA is telling humans and computers apart automatically. Captcha is a program which helps to protect your website from bots and determine the user is human or not. So, you need to add Captcha for protecting contact form, comment form and any other forms from spam. But many times you need to add a simple PHP captcha in form. This tutorial helps you to implement Captcha functionality using PHP in your website.

Sample Code

Captchagenerator.php we have created a Captcha PHP class which provides you highly customization option for PHP captcha. Using Captchagenerator script you can easily add captcha to the form. In the main Captchagenerator class contains 2 functions createCaptcha() and hexToRgb(). hexToRgb() function is used by createCaptcha() function, so you should need to call only createCaptcha() function.
  1. <?php
  2. class Captchagenerator
  3. {
  4. var $word = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  5. var $length = 5;
  6. var $img_width = 160;
  7. var $img_height = 50;
  8. var $font_path = '';
  9. var $font_size = 25;
  10. var $expiration = 7200;
  11. var $bg_color = '#ffffff';
  12. var $border_color = '#996666';
  13. var $text_color = '#cc9999';
  14. var $grid_color = '#ffb6b6';
  15. var $shadow_color = '#fff0f0';
  16.  
  17. public function __construct($config = array()){
  18. if (count($config) > 0){
  19. foreach ($config as $key => $val){
  20. if (isset($this->$key)){
  21. $method = 'set_'.$key;
  22. if (method_exists($this, $method)){
  23. $this->$method($val);
  24. }else{
  25. $this->$key = $val;
  26. }
  27. }
  28. }
  29. }
  30. if ( ! extension_loaded('gd')){
  31. return FALSE;
  32. }
  33. }
  34.  
  35. public function createCaptcha(){
  36. $str = '';
  37. for ($i = 0; $i < $this->length; $i++){
  38. $str .= substr($this->word, mt_rand(0, strlen($this->word) -1), 1);
  39. }
  40. $word = $str;
  41. $length = strlen($word);
  42. $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  43. $x_axis = rand(6, (360/$length)-16);
  44. $y_axis = ($angle >= 0 ) ? rand($this->img_height, $this->img_width) : rand(6, $this->img_height);
  45.  
  46. if (function_exists('imagecreatetruecolor')){
  47. $im = imagecreatetruecolor($this->img_width, $this->img_height);
  48. }else{
  49. $im = imagecreate($this->img_width, $this->img_height);
  50. }
  51. $bgColorRgb = $this->hexToRgb($this->bg_color);
  52. $borderColorRgb = $this->hexToRgb($this->border_color);
  53. $textColorRgb = $this->hexToRgb($this->text_color);
  54. $gridColorRgb = $this->hexToRgb($this->grid_color);
  55. $shadowColorRgb = $this->hexToRgb($this->shadow_color);
  56. $bg_color = imagecolorallocate ($im, $bgColorRgb[0], $bgColorRgb[1], $bgColorRgb[2]);
  57. $border_color = imagecolorallocate ($im, $borderColorRgb[0], $borderColorRgb[1], $borderColorRgb[2]);
  58. $text_color = imagecolorallocate ($im, $textColorRgb[0], $textColorRgb[1], $textColorRgb[2]);
  59. $grid_color = imagecolorallocate($im, $gridColorRgb[0], $gridColorRgb[1], $gridColorRgb[2]);
  60. $shadow_color = imagecolorallocate($im, $shadowColorRgb[0], $shadowColorRgb[1], $shadowColorRgb[2]);
  61.  
  62. ImageFilledRectangle($im, 0, 0, $this->img_width, $this->img_height, $bg_color);
  63. $theta = 1;
  64. $thetac = 7;
  65. $radius = 16;
  66. $circles = 20;
  67. $points = 32;
  68.  
  69. for ($i = 0; $i < ($circles * $points) - 1; $i++){
  70. $theta = $theta + $thetac;
  71. $rad = $radius * ($i / $points );
  72. $x = ($rad * cos($theta)) + $x_axis;
  73. $y = ($rad * sin($theta)) + $y_axis;
  74. $theta = $theta + $thetac;
  75. $rad1 = $radius * (($i + 1) / $points);
  76. $x1 = ($rad1 * cos($theta)) + $x_axis;
  77. $y1 = ($rad1 * sin($theta )) + $y_axis;
  78. imageline($im, $x, $y, $x1, $y1, $grid_color);
  79. $theta = $theta - $thetac;
  80. }
  81. $use_font = ($this->font_path != '' AND file_exists($this->font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  82. $x = rand(0, $this->img_width/($length/1.5));
  83. $y = $this->font_size+2;
  84.  
  85. for ($i = 0; $i < strlen($word); $i++)
  86. {
  87. if ($use_font == FALSE){
  88. $y = rand(0 , $this->img_height/2);
  89. imagestring($im, $this->font_size, $x, $y, substr($word, $i, 1), $text_color);
  90. $x += ($this->font_size);
  91. }else{
  92. $y = rand($this->img_height/2, $this->img_height-3);
  93. imagettftext($im, $this->font_size, $angle, $x, $y, $text_color, $this->font_path, substr($word, $i, 1));
  94. $x += $this->font_size;
  95. }
  96. }
  97. imagerectangle($im, 0, 0, $this->img_width-1, $this->img_height-1, $border_color);
  98. imagejpeg($im,NULL,90);
  99. header('Content-Type: image/jpeg');
  100. unset($_SESSION['captchaCode']);
  101. $_SESSION['captchaCode'] = $word;
  102. }
  103. public function hexToRgb($hex){
  104. $hex = str_replace("#", "", $hex);
  105. if(strlen($hex) == 3) {
  106. $r = hexdec(substr($hex,0,1).substr($hex,0,1));
  107. $g = hexdec(substr($hex,1,1).substr($hex,1,1));
  108. $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  109. } else {
  110. $r = hexdec(substr($hex,0,2));
  111. $g = hexdec(substr($hex,2,2));
  112. $b = hexdec(substr($hex,4,2));
  113. }
  114. $rgb = array($r, $g, $b);
  115. return $rgb;
  116. }
  117. }
  118. ?>
resultIndex.php this file is used to display the form and has a PHP Captcha script to provide and validate the captcha code.
  1. <?php
  2. if(isset($_POST['submit']) && !empty($_POST['submit'])){
  3. if(!empty($_POST['captcha_code'])){
  4. $captchaCode = $_SESSION['captchaCode'];
  5. $enteredcaptchaCode = $_POST['captcha_code'];
  6.  
  7. if($enteredcaptchaCode === $captchaCode){
  8. $succMsg = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Captcha Code Has Matched.';
  9. }else{
  10. $errMsg = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Please Try Again, Captcha Code Has Not Matched.';
  11. }
  12. }else{
  13. $errMsg = 'Please Enter The Captcha Code.';
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html>
  19. <head>
  20. <meta charset="UTF-8" />
  21. <title>Captcha Generator</title>
  22. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  23. <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
  24. <script src="js/jquery.min.js"></script>
  25. </head>
  26. <body>
  27. <div class="container">
  28. <div>
  29. <?php if(!empty($errMsg)) echo '<p style="color:#EA4335;">'.$errMsg.'</p>';?>
  30. <?php if(!empty($succMsg)) echo '<p style="color:#34A853;">'.$succMsg.'</p>';?>
  31. <img src="captcha.php" id="capImage"/>
  32. <br/>Can't Read The Image? Just Click<a href="javascript:void(0);" onclick="javascript:$('#capImage').attr('src','captcha.php');"><button class="btn btn-link">Refresh</button></a>.
  33. <form method="post">
  34. <br/>Enter The Code:<input class="form-control" name="captcha_code" type="text" value="">
  35. <input type="submit" class="btn btn-primary" name="submit" value="SUBMIT">
  36. </form><br/>
  37. </div>
  38. </body>
  39. </html>
Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.

Add new comment