Working on a PHP Contact Form with a user friendly captcha based on math

Working on a PHP Contact Form with a user friendly captcha based on math

Some times captcha can be very annoying and this is why i decided to build my own basic captcha with PHP. First of all I’d like to mention that my code is not very safe. (well either recaptcha can be hacked these days). However it worked for me and if you like you can copy paste it and improve it. Before you start make sure you are using PHP 5 and later.

So for the contact form i use two files. The first file called math_captcha.php and it generates the image. The second file with the source code i call it index.php but it can be named whatever.

Lets take a look on the captcha code that generates the image:

<?php

session_start();


$num1=rand(1,9); //Generate First number between 1 and 9  
$num2=rand(1,9); //Generate Second number between 1 and 9  
$captcha_total=$num1+$num2;  


$math = "$num1"." + "."$num2"."=";  

$_SESSION['rand_code'] = $captcha_total;

$dir = 'fonts/';

$image = imagecreatetruecolor(120, 25); //Change the numbers to adjust the size of the image
$color = imagecolorallocate($image, 200, 100, 90);

$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 20, 0, 20, 25, $color, $dir."Arial.ttf", $math );//Change the numbers to adjust the font-size


header("Content-type: image/png");
imagepng($image);

?>

And lets see the source code:

<?php
session_start();
	
  if(isset($_POST['submit'])) {
 
      $errors = array();
	  
      if($_POST['name'] == "") {
         $errors[] = 'The name field is empty';
      }
      if($_POST['email'] == "") {
         $errors[] = "The email field is empty";
      }
	   if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
          $errors[] = "The email address was not valid";
      }
	  if($_POST['subject'] == "") {
         $errors[] = "Please enter your subject";
      }
      if($_POST['comment'] == "") {
         $errors[] = "The comment field is empty";
      }
	   if ($_REQUEST['captcha_entered']!=$_SESSION['rand_code']) { 
	 	 $errors[] = "The math is incorrectly";
      }
      if(count($errors) == 0) {
         $sendto = "youremail@email.com";//Your email goes here
         $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
		 $email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
		 $subject = $_POST['subject'];//You can change your subject here
		 $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING);
			
		 //This is going to be the content to your Email
                 $message = "<strong>$name</strong> has sent you a message by using the contact form:
		
		<p><strong>Name:</strong> <em>$name</em></p>
		
                <p><strong>Email:</strong> <em>$email</em></p>
        
                <p><strong>The subject:</strong> <em>$subject</em></p>
		
		<p><strong>Message:</strong> <em>$comment</em></p>";
		
		$headers = "From: $name <$email> \r\n";
		$headers .= "X-Mailer:PHP/\r\n";
		$headers .= "MIME-Version:1.0\r\n";
		$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
 
         if(mail($sendto, $subject, $message, $headers)) {
             $success = true;
         } else {
             $success = false;
         }
    } else {
       $success = false;
 
    }
  }
 
  if(isset($_POST['submit'])) {
     if($success == true & count($errors) == 0) {
        echo "<script>alert('Thank you for your email $name, we will get back to you asap.');</script>";
     }
     if(count($errors) == 0 & $success == false & isset($_POST['submit'])) {
        echo "<h2>There was a problem with our form. Please email us directly via youremail@email.com.</h2>";
     }
 
     if($success == false & count($errors) > 0 & isset($_POST['submit'])) {
        echo '<ul>';
        foreach($errors as $show_all) {
           echo '<li><span style="color:#ff0000;">'.$show_all.'</span></li>';
        }
        echo '</ul>';
     }
 }
 
 ?>
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
   <p><label for="name">Name: </label><input type="text" name="name" placeholder="Name" id="name"></p>
   
   <p><label for="email">Email: </label><input type="text" name="email" placeholder="youremail@email.com" id="email"></p>
   
   <p><label for="subject">Subject: </label><input type="text" name="subject" placeholder="Subject" id="subject"></p>
   
   <p><label for="comment">Comment: </label><textarea name="comment" placeholder="Drop a line"></textarea></p>
     
<?php echo '<img src="math_captcha.php" />'; ?><input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" />
	 
   
   <p><input type="submit" name="submit" value="Submit"></p>
 </form>

PS: If I found free time I’ll work on the stylesheet of the form.

Comments

Leave a Reply