According to new research, many hackers are working together to hack many sites. Now everyone is familiar with PHP, xHTML, CSS working and they can hack your form by
- Save page as
- Code checking
- Firebug tool
- Ctrl+U
So you need to make your contact form secure to safe your information..
I made this form to make your site more secure.
This form consists on some parts:
- Validate Function
- Values Validating
- Characters Limit Error Message
- Left Field Error Message
- Auto Selected fields after redirection
- CSS
- Form Fields
- Form Results
- Form Error Message
This form is divided into parts, anyone can easily understand it.
In this form : characters limit is
'name' => 20, 'number' => 16, 'email' => 35,'subject' => 50, 'message' => 350, 'website' => 50
You can change it according to yourself.
<?php
#######################################
# Form Validation Function Start #
#######################################
function form_validation($input)
{
$input = trim($input);
$input = htmlspecialchars($input);
$input = stripcslashes($input);
return $input;
}
function validate_number($input_number)
{
$input_number = preg_match ("/^[0-9]*$/", $input_number);
$input_number = trim($input_number);
$input_number = htmlspecialchars($input_number);
$input_number = stripcslashes($input_number);
return $input_number;
}
function validate_name($input_name)
{
$input_name = preg_match("/^[a-zA-Z ]*$/", $input_name);
$input_name = trim($input_name);
$input_name = htmlspecialchars($input_name);
$input_name = stripcslashes($input_name);
return $input_name;
}
function validate_email($input_email)
{
$input_email = preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $input_email);
$input_email = trim($input_email);
$input_email = htmlspecialchars($input_email);
$input_email = stripcslashes($input_email);
return $input_email;
}
function validate_web($input_web)
{
$input_web = preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $input_web);
$input_web = trim($input_web);
$input_web = htmlspecialchars($input_web);
$input_web = stripcslashes($input_web);
return $input_web;
}
######################################
# Form Validation Function end #
######################################
/*--------------------------------------------------------------------------------*/
####################################################
# Getting All Values and Validating them Start #
####################################################
$error_msg = ""; //this variable for error massage
$departments = array(); //this array for saving department
$name = $number = $email = $birth = $gender = $subject = $website = $select = $message = $color = ''; // we show all variables empty
###############################################################
# Submit Button Processing with secure validation Start #
###############################################################
if(isset($_POST['submit']))
{
$fields = array('name', 'number', 'email', 'birth', 'gender', 'departments', 'select', 'website', 'subject', 'message', 'color'); // all values field array
$errors = array(); //this array for saving errors
foreach ($fields as $field) //this is for getting values
{
if(!isset($_POST[$field]) || $_POST[$field] == '' && $field != 'color') //if field value is empty or has nothing
{
array_push($errors, $field); //then selective field array value will be saved in error array.
}
else //if submit button has run, then it else will run
{
if($field != 'departments') //but if value is other than department field
{
$$field = form_validation($_POST[$field]); //It will go through user define function and value will be made variables
}
elseif($field == 'departments') //but if value is about department then
{
$departments = array(); //we define an array to save department values
$test_dept = $_POST[$field]; //we get department here
foreach($test_dept as $dept ) //we used departments parameter to save that on dept
{
array_push($departments, form_validation($dept)); //department will be fully validate and saved to department array
}
}
}
if($field == 'select' && $_POST[$field] == 'Navigation')
{
!array_push($errors, form_validation($field));
}
} //this is foreach loop end
##################################################
# Getting All Values and Validating them end #
##################################################
###########################################################################
# If No Error than Check characters Limit and show exceed Massage Start #
###########################################################################
//As we have saved error values in errors array
if(empty($errors) || !empty($errors))
{
//we define a assosiative array to define characters limit
$fields = array('name' => 20, 'number' => 16, 'email' => 35,'subject' => 50, 'message' => 350, 'website' => 50);
//For checking lenght we need to add a loop
foreach($fields as $field => $length) //foreach loop is used for arrays : $field => $length we used for assosiative array
## in this $field => $length : $length variable as assigned to values
{
if (strlen($_POST[$field]) > $length)
{
array_push($errors, $field . ' field characters limit has exceeded.'); //we shall use $field variable here,
# Because we are not using $fields, we are using $field as referance.
}
if($field == 'number' && !validate_number(($_POST[$field]))) //it is to validate number field.
{
array_push($errors, $field . " is not proper number."); //if there is an error in number field , error would be push on error array
}
if($field == 'name' && !validate_name(($_POST[$field]))) //it is to validate number field.
{
array_push($errors, $field . " is not proper name."); //if there is an error in number field , error would be push on error array
}
if($field == 'email' && !validate_email(($_POST[$field])) && !empty($_POST[$field])) //it is to validate number field.
{
array_push($errors, $field . " is not proper email."); //if there is an error in number field , error would be push on error array
}
if($field == 'website' && !validate_web(($_POST[$field])) && !empty($_POST[$field])) //it is to validate number field.
{
array_push($errors, $field . " is not proper website link."); //if there is an error in number field , error would be push on error array
}
}
} //it is if emtpy end
##########################################################################
# If No Error than Check characters Limit and show exceed Massage End #
##########################################################################
#####################################################
# If Error than then show the errors fields start #
#####################################################
if(!empty($errors))
{
$error_msg = '<b>There are errors in following fields:</b> <br />';
$error_msg .= implode('<br />', $errors);
} //it is if not empty end
#########################################################
# If Error than then show the errors fields end #
#########################################################
} //if isset POST['submit'] end
###############################################################
# Submit Button Processing with secure validation End #
###############################################################
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="us-en">
<head>
<title><?php $pagetitle="Self Controlled Form : Full Contact Form Feachers : Samee Ullah Feroz";
define("blogname","www.QWC.me");
echo $pagetitle ." : ". blogname; ?></title>
<meta name="description" content="<?php echo $pagetitle ?> : Buy it." />
<link rel="icon" type="image/ico" href="http://www.iconarchive.com/download/i50954/deleket/3d-cartoon-vol3/Web-Coding.ico" alt="Icon" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/*html
{
height:100%;
width:100%;
background:url(http://www.southsoft.co.za/images/mysql.jpg) center center no-repeat;
background-position:center;
You can also give local file path
}*/
a:link
{
color:#00F;
text-decoration:none;
}
body
{
color:#0000FF;
font-family:"Courier New", Courier, monospace;
/*background-image:url(http://www.getacho.com/download/phpcss/images/bg-color.png);
background-repeat:repeat;
background-attachment:fixed;*/
}
#main
{
color: #0000FF;
overflow: auto;
padding: 10px;
width: 100%;
}
ul
{
list-style:square url("images/sqpurple.gif");
}
#table_bg
{
border:0;
border-color:#000;
border-collapse:separate;
padding:3;
padding-right:100px;
}
.sidemsg
{
margin-top:60px;
font-family:"Courier New", Courier, monospace;
font-size:16px;
}
</style>
</head>
<body style="background-color:<?php echo $color?>; ">
<div id="main">
<h1 align="center">Secure Self Controlled Contact Form.</h1>
<h2>Qualities</h2>
<ul>
<li>Secure from Hackers</li>
<li>Fast Processing</li>
<li>Single File Contact Form</li>
<li>Show Error on characters limit exceeded</li>
<li>Show Error on Wrong Name</li>
<li>Show Error on Wrong Email</li>
<li>Show Error on Wrong Website</li>
<li>Show Error on Wrong number</li>
<li>Numbers only digits not doubles</li>
<li>Cannot select future date of birth</li>
<li>Can select background custom color</li>
<li>Can select required Department</li>
<li>Can select required Agent</li>
<li>HTML, Javascript etc. Don't work</li>
<li>After getting errors, form will not reset</li>
</ul>
<blockquote>This form is coded to keep away your site from <b>Hackers</b>.</blockquote>
<h2>Price</h2>
<hp>You can purchase this form in 5$ just.</hp>
<b>Samee is Online On</b><br />
<b>Company Site : </b><a href="http://www.getacho.com" target="_blank">Getacho Company</a><br />
<b>Facebook : </b><a href="http://www.facebook.com/sameeullah.feroz" target="_blank">Samee Ullah Feroz</a><br />
<b>Gtalk :</b> <a href="mailto:seo.getacho@gmail.com" target="_blank">SEO.Getacho</a><br />
<b>Skype : </b><a href="skype:SEO.Getacho?call">SEO.Getacho</a><br />
</div>
<div id="table_bg">
<table style="background-color:<?php echo $color?>; width: 100%;" align="center" >
<form action="self_controlled_form.php" method="post" >
<tr><td><label for="name"><strong>Full Name:</strong></label></td>
<td><input type="text" name="name" placeholder="Full Name" style="width:250px;" value="<?php echo $name;?>" /></td></tr>
<tr><td><label for="number"><strong>Personal Number:</strong></label></td>
<td><input type="text" name="number" placeholder="00923234223945" style="width:250px;" value="<?php echo $number;?>"/></td></tr>
<tr><td><label for="email"><strong>Email:</strong></label></td>
<td><input type="text" name="email" placeholder="i.e. mail@example.com" style="width:250px;" value="<?php echo $email;?>"/></td></tr>
<tr><td><label for="website"><strong>Website:</strong></label></td>
<td><input type="text" name="website" placeholder="i.e. www.example.com" style="width:250px;" value="<?php echo $website;?>"/></td></tr
><tr><td><label for="gender"><strong>Your Gender:</strong></label></td>
<td><input type="radio" name="gender" value="Male"
<?php if($gender == 'Male' && !empty($gender))echo 'checked';?>/>Male
<input type="radio" name="gender" value="Female"
<?php if($gender == 'Female' && !empty($gender))echo 'checked';?>/>Female</td></tr>
<tr><td><label for="birth"><strong>Your DOB:</strong></label></td>
<td><input type="date" name="birth" value="<?php echo $birth;?>" max="<?php echo date();?>" min="1991-01-01" /></td></tr>
<tr><td><label for="departments[]"><strong>Contact Departments:</strong></label></td>
<td><input type="checkbox" name="departments[]" value="Marketing Department"
<?php if(in_array('Marketing Department', $departments)&& !empty($departments))echo 'checked';?>/>Marketing Department <br />
<input type="checkbox" name="departments[]" value="Development Department"
<?php if(in_array('Development Department',$departments)&& !empty($departments))echo 'checked';?>/>Development Department <br />
<input type="checkbox" name="departments[]" value="Finance Department"
<?php if(in_array('Finance Department', $departments) && !empty($departments))echo 'checked';?>/>Finance Department </td></tr>
<tr><td><label for="select"><strong>Contact Agent:</strong></label></td>
<td><select name="select">
<option value="Navigation">Navigation</option>
<option value="Online Agent" <?php if($select == 'Online Agent' && !empty($select))echo 'selected'; ?> >Online Agent</option>
<option value="SEO, SMO Agent" <?php if($select == 'SEO, SMO Agent' && !empty($select))echo 'selected'; ?> >SEO, SMO Agent</option>
<option value="SEM, SMM Agent" <?php if($select == 'SEM, SMM Agent' && !empty($select))echo 'selected'; ?> >SEM, SMM Agent</option>
<option value="Development Agent" <?php if($select == 'Development Agent' && !empty($select))echo 'selected'; ?> >Development Agent</option>
<option value="Consultancy Agent" <?php if($select == 'Consultancy Agent' && !empty($select))echo 'selected'; ?> >Consultancy Agent</option>
<option value="HR Agent" <?php if($select == 'HR Agent' && !empty($select))echo 'selected'; ?> >HR Agent</option>
</select></td></tr>
<tr><td><label for="subject"><strong>Subject:</strong></label></td>
<td><input type="text" name="subject" placeholder="Type Subject" style="width:250px;" value="<?php echo $subject;?>"/></td></tr>
<tr><td valign="top" align="left"><label for="message"><strong>Your Message:</strong></label></td>
<td><textarea cols="40" rows="10" name="message" placeholder="Write your message"><?php echo $message;?></textarea></td></td>
<tr><td valign="top" align="right"><input type="submit" value="Send" name="submit" /></td>
<td>Choose the color :<input type="color" name="color" value="<?php echo $color; ?>" />
<br />This Feature Works on Chrome.
<br />Developed by <a href="mailto:sam@qwc.me">Samee Ullah Feroz</a><br /> Powered by : <a href="http://www.qwc.me">QWC.Me</a></td></tr>
</form>
</table>
</div>
<div class="sidemsg" style="background-color:<?php echo $color?>; ">
<h2>Results Here</h2>
<?php
echo $name . "<br />";
echo $number . "<br />";
echo $email . "<br />";
echo $website . "<br />";
echo $gender . "<br />";
echo $birth . "<br />";
if (!empty($departments))
{
echo implode("<br />", $departments) . "<br />";
}
echo $select . "<br />";
echo $subject . "<br />";
echo $message . "<br />";
echo $error_msg . "<br />";
?>
</div>
</body>
</html>
Form is complete with full security and limitations.
Instructions : This code is in-complete if you need this form, contact
Samee Ullah Feroz
Demo :