Web2 style secure & flexible free php contact form with easy setup
This a small and powerful contact form, which protected with a random colored captcha. You can start to use it with changing just ADMIN_EMAIL. You can easily add-remove contact fields like name, email, homepage, phone number, fax, address, subject, message etc. You can easily set required fields, which visitors must fill. Required fields are red marked when visitors try to send form with empty values. It is flexible; you can change filename or include it in your cms modules. Because of more security it uses session for validation not cookies. There is also a small brute-force protection included. A small donation or backlink wished but not required. Demo in action! (Download link is below)
Code of PHP-File:
<?php
/**
* powered by @cafewebmaster.com
* free for private use
* please donate by paypal or give a backlink
*/
// Configuration
define("ADMIN_MAIL", "your_email@example.com");
define("MAIL_SUBJECT", "Message over Contact-Form from Your Website");
// one line input fields like name, phone, fax, website etc, just add or remove what do you want
$contact_fields_input = array("Name", "Website", "E-Mail", "Phone_Number", "Subject"); // no spaces, no symbols
// multiline input field(s), just add or remove what do you want
$contact_fields_textarea = array("Message");
// required fields, visitor must fill to send form
$contact_fields_required = array("Name", "Website", "E-Mail", "Phone_Number", "Message");
// You dont need to change below
###############################################################################
foreach($_GET as $k=>$v){
$req_string .= "".$k."=".$v."&";
}
$selfurl = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$req_string ;
session_start();
function playCaptcha(){
$text = rand(999,9999); // 4 chars number
$seckey = md5(strtolower($text));
$_SESSION["seckey"] = $seckey;
$img_w = 80;
$img_h = 30;
$left = 5;
$top = 2;
$font = 'verdana.ttf';
$font_size = 6;
$sleep = 1; // against brute-force
$imgd = imagecreate($img_w, $img_h);
$bg_light = imagecolorallocate($imgd, rand(200,255), rand(200,255), rand(200,255));
$red = imagecolorallocate($imgd, 255, 0, 0);
imagefill($imgd, 0, 0, $bg_light);
$polight = imagecolorallocate($imgd, rand(155,240), rand(155,240), rand(155,240));
$points = array( rand(0,$img_w), rand(0,$img_h), rand(0,$img_w), rand(0,$img_h),
rand(0,$img_w), rand(0,$img_h), rand(0,$img_w), rand(0,$img_h), rand(0,$img_w), rand(0,$img_h));
imagefilledpolygon($imgd, $points, 5, $polight);
while($i2<10){ $i2++;
$clight = imagecolorallocate($imgd, rand(155,240), rand(155,240), rand(155,240));
$r1 = rand(0,$img_w); $r2 = rand(0,$img_h); $r3 = rand(0,$img_w); $r4 = rand(0,$img_h);
imageline ( $imgd, $r1, $r2, $r3, $r4, $clight);
# imageline ( $imgd, $r1+1, $r2, $r3+1, $r4, $clight);
}
while( $i < strlen($text) ){ $i++;
$darkcolor = imagecolorallocate($imgd, rand(0,111), rand(0,222), rand(0,222));
$current_letter = substr($text, $i-1, 1);
$font2 = $font;
# if( is_numeric($current_letter) ){ $darkcolor = $red ; $font2 = 'times.ttf'; }
# imagettftext($imgd, $font_size, rand(-45,45), $left+($i*30), $top+rand(25,30), $darkcolor, $font2, $current_letter);
imagestring($imgd, $font_size, $left+(($i-1)*20), $top+rand(1,5), $current_letter, $darkcolor);
}
sleep($sleep);
header("Pragma: no-cache");
header("Content-type: image/jpg");
ImageJPEG( $imgd );
imagedestroy($imgd);
exit;
} // end of playCaptcha
function displayContactForm(){
global $selfurl, $contact_fields_input, $contact_fields_textarea, $contact_fields_required ;
foreach($contact_fields_input as $v){
$redmark = ($_POST && in_array($v, $contact_fields_required) && !$_POST[$v]) ? "redmark" : "" ;
if($redmark) $error_req++;
$required = in_array($v, $contact_fields_required) ? "*" : "" ;
$htmo_form .= "<p><label for=\"$v\">$v $required :</label>
<input name=\"$v\" size=\"60\" value=\"".$_POST[$v]."\" class=\"contactFields $redmark\" /></p>";
}
foreach($contact_fields_textarea as $v) {
$redmark = ($_POST && in_array($v, $contact_fields_required) && !$_POST[$v]) ? "redmark" : "" ;
if($redmark) $error_req++;
$required = in_array($v, $contact_fields_required) ? "*" : "" ;
$htmo_form .= "<p><label for=\"$v\">$v $required :</label>
<textarea name=\"$v\" cols=\"60\" rows=\"20\" class=\"contactFields $redmark\">$_POST[$v]</textarea></p>";
}
$redmark = ($_POST && $_SESSION["seckey"] != md5($_POST['captcha'])) ? "redmark" : "" ;
if($redmark) $error_req++;
$htmo_form .= "<p><label for=\"captcha\">Captcha:</label>
<img src=\"".$selfurl."yekta=captcha\" height=\"30\" width=\"80\" align=\"absmiddle\">
»»
<input name=\"captcha\" class=\"captcha $redmark\" />
<input type=\"submit\" class=\"submit\" /></p>";
if($_POST && !$error_req){
foreach($_POST as $k=>$v){
if($k == "captcha") continue;
$mailbody .= "$k : \n$v\n\n";
}
if(@mail(ADMIN_MAIL, MAIL_SUBJECT, $mailbody, "From: ".ADMIN_MAIL."\r\n")) {
$htmo_form = "<p>Your eMail has been sent. Thank you!</p>";
} else {
$htmo_form = "<p class=\"redmark\">Error: Mail could not been sent!</p>";
}
}
echo <<<cafewebmaster_com
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style type="text/css">
#cform { margin: auto; width: 460px; background-color: #eee; padding: 10px; font: bold 12px verdana; }
#cform img { border: 1px solid #000; }
#cform .redmark { border: 2px solid #f00; }
#cform h1 { background-color: #036; padding: 5px; margin: 0; color: #abc; }
#cform p { background-color: #ddd; padding: 5px ; }
#cform label { display: block; float: left; width: 140px; clear: left; }
#cform input.contactFields { width: 300px; }
#cform input.submit { width: 120px; }
#cform input.captcha { width: 40px; }
#cform textarea.contactFields { width: 300px; height: 12em; }
</style>
</head>
<body>
<div id="cform">
<h1>Contact us!</h1>
<form method="post" action="$selfurl">$htmo_form</form>
<div style="text-align: right; font: 9px verdana;">Powered by <a href="http://www.cafewebmaster.com" title="Wemaster resources">CafeWebmaster</a></div>
</div>
</body></html>
cafewebmaster_com;
}
switch($_GET['yekta']){
case "captcha":
playcaptcha();
break;
default: displayContactForm();
}
| Attachment | Size |
|---|---|
| Download free secure_contact_form.zip | 2.36 KB |
Similar entries
- Common HTML / Web colors
- Web2 Style CSS Dynamic Menu with Arrows and Background-Images
- DropDown / Rollover Menu with pure CSS / HTML
- Pure CSS Mouseover Menu without Javascript
- Upload multiple images with PHP
- Mouseover images with CSS
- Web 2.0 Style two Side Background, Dark to Light Effect
- Search in text files recursively with PHP - Grep
- need help for customizing PHP-grep files search
- Smart Multi-Uploader and Thumbnail Creator from GIF/ JPEG/ PNG with PHP
- A small login to add to top of online testing scripts
- Repair all mysql databases-tables with PHP
- Cool Drupal Themes with preview to download directly
- How to filter all html tags from each _GET and _POST request
- CSS Text effects for MSIE
- Alternative captcha ideas for future
- Redirect a query to multiple search engines with one form and javascript
- Photo Album / Picture Gallery Script with PHP

Comments
Test this form...
testing contact form...
From and Replay:to
Same as Urmel, I don't know how to replace .ADMIN_MAIL. with the visitors e-mail as From: and Replay:to
This script has great Captcha, but e-mail sending options should be more visible and simple to modify.
Test
Is this going to work?
Post new comment