PHP CAPTCHA script

PHP CAPTCHA script
PHP CAPTCHA script

phpTextCaptcha is an open-source, free PHP CAPTCHA script to create complex images and protect forms from spam and abuse.

Features

  1. Small code structure.
  2. It's using phptextClass to create a captcha image.
  3. Customizable code.
  4. Security features, such as random lines and noise.

Live Demo Download Script

  phptextClass.php

Main class file with phpcaptcha, hexToRGB and ImageTTFCenter function.

  <?php  /*phptext class, version 1.0  created by www.w3schools.in (Gautam kumar)  April 26, 2014  */  class phptextClass  {          public function phpcaptcha($textColor,$backgroundColor,$imgWidth,$imgHeight,$noiceLines=0,$noiceDots=0,$noiceColor='#162453')          {                         /* Settings */                  $text=$this->random();                  $font = './font/monofont.ttf';/* font */                  $textColor=$this->hexToRGB($textColor);                        $fontSize = $imgHeight * 0.75;                                    $im = imagecreatetruecolor($imgWidth, $imgHeight);                        $textColor = imagecolorallocate($im, $textColor['r'],$textColor['g'],$textColor['b']);                                                      $backgroundColor = $this->hexToRGB($backgroundColor);                  $backgroundColor = imagecolorallocate($im, $backgroundColor['r'],$backgroundColor['g'],$backgroundColor['b']);                                                    /* generating lines randomly in background of image */                  if($noiceLines>0){                  $noiceColor=$this->hexToRGB($noiceColor);                      $noiceColor = imagecolorallocate($im, $noiceColor['r'],$noiceColor['g'],$noiceColor['b']);                  for( $i=0; $i<$noiceLines; $i++ ) {                                                    imageline($im, mt_rand(0,$imgWidth), mt_rand(0,$imgHeight),                          mt_rand(0,$imgWidth), mt_rand(0,$imgHeight), $noiceColor);                  }}                                                                                  if($noiceDots>0){/* generating the dots randomly in background */                  for( $i=0; $i<$noiceDots; $i++ ) {                          imagefilledellipse($im, mt_rand(0,$imgWidth),                          mt_rand(0,$imgHeight), 3, 3, $textColor);                  }}                                                  imagefill($im,0,0,$backgroundColor);                      list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);                    imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);                                  imagejpeg($im,NULL,90);/* Showing image */                  header('Content-Type: image/jpeg');/* defining the image type to be shown in browser widow */                  imagedestroy($im);/* Destroying image instance */                  if(isset($_SESSION)){                          $_SESSION['captcha_code'] = $text;/* set random text in session for captcha validation*/                  }          }                    /*function to convert hex value to rgb array*/          protected function hexToRGB($colour)          {          if ( $colour[0] == '#' ) {                          $colour = substr( $colour, 1 );          }          if ( strlen( $colour ) == 6 ) {                          list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );          } elseif ( strlen( $colour ) == 3 ) {                          list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );          } else {                          return false;          }          $r = hexdec( $r );          $g = hexdec( $g );          $b = hexdec( $b );          return array( 'r' => $r, 'g' => $g, 'b' => $b );          }                                              /*function to get center position on image*/          protected function ImageTTFCenter($image, $text, $font, $size, $angle = 8)           {                  $xi = imagesx($image);                  $yi = imagesy($image);                  $box = imagettfbbox($size, $angle, $font, $text);                  $xr = abs(max($box[2], $box[4]))+5;                  $yr = abs(max($box[5], $box[7]));                  $x = intval(($xi - $xr) / 2);                  $y = intval(($yi + $yr) / 2);                  return array($x, $y);             }    }  ?>

demo.php

This file is used to obtain submitted form data and used to validate the captcha image.

  <?php session_start();    if(isset($_POST['Submit'])){          // code for check server side validation          if(empty($_SESSION['captcha_code'] ) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0){                    $msg="<span style='color:red'>The Validation code does not match!</span>";// Captcha verification is incorrect.                       }else{// Captcha verification is Correct. Final Code Execute here!                                $msg="<span style='color:green'>The Validation code has been matched.</span>";                        }  }         ?>        <form action="" method="post" name="form1" id="form1" >    <table width="400" border="0" align="center" cellpadding="5" cellspacing="1" class="table">      <?php if(isset($msg)){?>      <tr>        <td colspan="2" align="center" valign="top"><?php echo $msg;?></td>      </tr>      <?php } ?>      <tr>        <td align="right" valign="top"> Validation code:</td>        <td><img src="captcha.php?rand=<?php echo rand();?>" id='captchaimg'><br>          <label for='message'>Enter the code above here :</label>          <br>          <input id="captcha_code" name="captcha_code" type="text">          <br>          Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh.</td>      </tr>      <tr>        <td> </td>        <td><input name="Submit" type="submit" onclick="return validate();" value="Submit" class="button1"></td>      </tr>    </table>  </form>

captcha.php

This file is used to generate a captcha image.

  <?php          session_start();          include("./phptextClass.php");                      /*create class object*/          $phptextObj = new phptextClass();                 /*phptext function to genrate image with text*/          $phptextObj->phpcaptcha('#162453','#fff',120,40,10,25);         ?>

Enjoyed reading this? Check out other exciting contents.
  1. Php Code for View, Search, Edit and Delete Record from MySql Table
  2. Php Code for View, Search, Edit and Delete Record from MySql Table
  3. Php code for Comment on Webpage
  4. Php code for Online Quiz
  5. Php code for Online Quiz
  6. Php code example for login
  7. Send E-mail to Multiple Person(E-mail) with Php
  8. Php code example for Secure login

Content Sources https://educratsweb.com/3212-content.htm

if you want to share your story or article for our Blog please email us at educratsweb@gmail.com or Click here

Post a Comment

0 Comments