PHP Security ImagesCaptcha

PHP security imagesCaptcha

PHP Security imagesCaptcha is a simple test to determine if a user is a computer or a human.  It is used to prevent spam abuse on the websites. Just Avoid spamers and crackers in the sites. So if you use CAPTCHA on your web forms, this can help in stopping some bots and making life harder for other bots in accessing or using your forms. Normal Core PHP simple Shopping Cart

PHP security imagesCaptcha
PHP security imagesCaptcha


Captcha.php

<?php
session_start();
class CaptchaSecurityImages {

 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 CaptchaSecurityImages($width='60',$height='50',$characters='5') {
  $code = $this->generateCode($characters);
  /* font size will be 75% of the image height */
  $font_size = $height * 0.85;
  $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, 200, 200, 200);
  /* 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['keycode'] = $code;
 }

}

$width = isset($_GET['width']) ? $_GET['width'] : '90';
$height = isset($_GET['height']) ? $_GET['height'] : '21';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '5';

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

?>
Your Form Page Added Following Script.
<script type="text/javascript">

function refreshCaptcha(abspath){

document.getElementById('refcaptimg').src="Captcha.php?t=" + new Date().getTime();

}</script>
Form

<form name="frmContact" method="post" action="contact-us.html">
<table width="175" border="0" cellpadding="2" cellspacing="0">
 <tr>
   <td  height="81" align="left" valign="middle"><a href="full_certificate.pdf" target="_blank"><img src="images/svc.gif" alt="" border="0"/></a></td>

  </tr>
<tr>

<td style="padding-bottom:10px;">
<div>
<!-- End Comm100 Live Chat Button Code -->

</td>
  </tr>

  <tr>
    <td class="page_heading"> </td>
  </tr>
  <tr>

   <td class="fnt_sml" style="padding-top:10px;"><img src="images/FORM/OPT-1.png" width="39" height="8" /><span style="color:#FF0000"></span></td>
  </tr>
  <tr>
   <td  ><input type="text" name="Name" id="Name" class="input_left" value='<?php echo $LastName?>' />
<span style="clear:left;" class="input_left_error" ><?php echo $msgName?></span></td>
  </tr>
 <tr>
    <td class="fnt_sml"><img src="images/FORM/OPT-3.png" /></span></td>

  </tr>
  <tr>
    <td><input type="text" name="Email" id="Email" class="input_left" value='<?php echo $Email ?>' />

<span style="clear:left;" class="input_left_error" ><?php echo $msgEmail ?></span></td>

  </tr>
  <tr>
    <td class="fnt_sml"><img src="images/FORM/OPT-4.png" /></td>
  </tr>
  <tr>
    <td><input type="text" name="Phone" id="Phone" class="input_left" value='<?php echo $Phone ?>' />

<span style="clear:left;" class="input_left_error" ><?php echo $msgPhone ?></span></td>

  </tr>
  <tr>
   <td class="fnt_sml"><img src="images/FORM/OPT-5.png" width="57" height="10" /></td>

  </tr>
  <tr>
    <td class="fnt_sml"><input type="text" name="TelPhone" class="input_left" id="TelPhone" value='<?php echo $TelPhone?>' />
    <span style="clear:left;" class="input_left_error"><?php echo $msgTelPhone ?></span></td>
  </tr>
 <tr>
   <td class="fnt_sml"><img src="images/FORM/OPT-2.png" /></td>

  </tr>
  <tr>
    <td><textarea name="Message" id="Message" rows="5" cols="25" class="input_left" value='<?php echo $Message?>'></textarea>

<span style="clear:left;" class="input_left_error" ><?php echo $msgMessage ?></span></td>
   </tr>
  <tr>
    <td class="fnt_sml"><img src="images/FORM/OPT-6.png" width="89" height="8" /></td>

  </tr>
  <tr>
    <td align="center"><input type="text" name="keycode" id="keycode" size="20" class="input_left" style="width:170px;" value='<?php echo $keycode ?>' />

<span style="clear:left;" class="input_left_error" > <?php echo $msgkeycode ?> </span>

<img src="CaptchaSecurityImages.php" id="refcaptimg" align="middle" style="margin-top:5px;" alt="" />&nbsp;<a href="javascript:refreshCaptcha('images/')"><img  src="images/hip_reload.gif" width="22" height="22" border="0" align="middle" style="margin-top:1px;" alt="" /></a>
          <a href="javascript:refreshCaptcha('images/')"><span class="style1" style="color: #FE4819"></span></a>

      <div class="input_left_error"></div></td>
  </tr>
  <tr>
    <td align="center"><input type="submit" name="Submit" value="Submit" style="background-color:#2172B8; color:#ffffff; border:solid 1px #2172B8;" />
</td>
</tr>
</table>
</form>

PHP Code Follow us : Contact-us.php

<?php if(isset($_POST[Submit])){

if(empty($_POST[Name])){$msgName='Please Enter Your Name.'; $Name= false;}
 else{$Name=$_POST[Name];}

if(empty($_POST[Message])) {$msgMessage= 'Please Enter your message.'; $Message=false; }
       else{$Message=$_POST[Message];}

if(empty($_POST[Email])){$msgEmail='Please Enter Your Email-Id';$Email= false; }
 else{$Email=$_POST[Email];}
 if (!eregi("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$", $_POST[Email])){ $msgEmail= 'Please Enter A Valid Email Address'; $Email = false; }
 else{$Email=$_POST[Email];}

if(empty($_POST[Phone])){$msgPhone='Please Enter Your Contact No. '; $Phone= false;}
 else{$Phone=$_POST[Phone];}
 if(!eregi("[0-9.-\+]", $_POST[Phone])) {$msgPhone='Please enter a valid contact no. '; $Phone=false;}
 else{ $Phone=$_POST[Phone];}

 $TelPhone=$_POST[TelPhone];

if($_POST[keycode]!=$_SESSION[keycode] OR $_SESSION[keycode]==''){$msgkeycode = 'Incorrect verification code';$keycode = false;}
 else{$keycode=$_POST[keycode];}
 if($Name  && $Message && $Email && $Phone && $Message && $keycode)
 {
  header("Location: $tpage");
exit;
}else{ index.php } ?>
PHP Security ImagesCaptcha Dev2Tricks 5 of 5
PHP security imagesCaptcha PHP Security imagesCaptcha is a simple test to determine if a user is a computer or a human.  It is used to p...

Share this

Related Posts

Previous
Next Post »