|
The following scripts allow the programmer to add a Formail to a website using the mail() program Form mail program with additional security to stop malicious code been injected into the header. Also contains cross-site request forgery (CSRF) session matching to stop CSRF Antispam is provided by google captcha. Input is sanitized to avoid malicious input by filtering all the formmail inputs |
| index.php Program to display
Form mail and include various additional php programs to
filter input to the form
Also
passes thro a token in a hidden field that is
produced in the extra.php script, this is checked
to equal in the check script.
Antispam is provided by google captcha., you will need a google account to be able to get the keys to use it. Finally the input is sanitized to avoid malicious input by filtering all the formmail inputs |
|
<html lang="en"> <head> <meta charset="utf-8"> <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)"> <meta name="dcterms.created" content="Fri, 20 Nov 2020 19:22:45 GMT"> <meta name="description" content=""> <meta name="keywords" content=""> <title></title> <link rel="stylesheet" type="text/css" href="style.css" /> <link href="style.css" rel=”stylesheet” type="text/css" /> <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,900' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css"> <!--[if IE]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body class="bg-dark"> <?php if(file_exists("check.php")) { } else { echo"file is not found"; } include("captcha.php"); include("extra.php"); include("check.php"); include_once("send.php"); ?> <div id="container"> <div class="row justify-content-center"> <div class="col-md-6 m-5 p-2 bg-light rounded"> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> <span class="error"> <?php echo $all;?></span> <span class="error"> <?php echo $successErr;?></span> <hr class="bg-light"> <center> <p><span class="error">* required field</span></p></center> <span class="input-group-text"> <i class="fas fa-user"></i> <input type="text" name="name" id="Name" value="<?= $name?>" placeholder="*Full Name" /><br></span> <span class="error"> <?php echo $nameErr;?></span> <span class="input-group-text "> <i class="fas fa-envelope"></i> <input type="text" name="email" id="Email" value="<?= $email?>" placeholder="*Email"/><br></span> <span class="error"> <?php echo $emailErr;?></span> <span class="input-group-text"> <i class="fa fa-phone"></i> <input type="text" name="tel" id="Phone" value="<?= $tel?>" placeholder="Phone"/><br></span> <span class="error"><?php echo $telErr;?></span> <span class="input-group-text"> <textarea name="comment" rows="10" cols="30" id="Message" value="" placeholder="*Message"><?php echo $comment?></textarea></span> <span class="error"><?php echo $commentErr;?></span> <input type="hidden" name="token" value="<?=$_SESSION['token']?>"/> <span class="error"><?php echo $spamErr;?></span> <center><div class="g-recaptcha" data-sitekey="<?php print $public_key; ?>"></div></center> <center> <input type="submit" name="submit" value="Submit" class="submit-button"/></center> </form> <script src='https://www.google.com/recaptcha/api.js'></script> </div> <!-- end of col --> </div> <!-- end of row --> </div> <!-- end container--> </body> </html> |
| check.php Function checks
and sanitizes inputs for valid data and checks for
injection of malicious content
|
<!DOCTYPE HTML> <html> <head> <style> .error {color: black;} </style> </head> <body> <?php // define variables and set to empty values $all = $commentErr = $successErr = $error = $nameErr = $emailErr = $telErr = $spamErr = ""; $name = $email = $tel = $body = $comment = ""; $EmailFrom = "sales@my_website"; $EmailTo = "email_to_go_to@yahoo.co.uk"; $Subject = "Simple Contact Form"; if (isset($_POST["name"])) { $name = test_input($_POST["name"]); } if (isset($_POST["email"])) { $email = test_input($_POST["email"]); } if (isset($_POST["tel"])) { $tel = test_input($_POST["tel"]); } if (isset($_POST["comment"])) { $comment = test_input($_POST["comment"]); } //$body = test_input($_POST["comment"]); if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; $errorname = true; } else if (strlen($name) < 3) { $errorname = true; $nameErr = "Name must have at least 3 characters."; } else if (strlen($name) > 50) { $errorname = true; $nameErr = "You have a limit of 50 chars."; } else if (!preg_match("/^[a-zA-Z ]+$/",$name)) { $errorname = true; $nameErr = "Name must contain alphabets and space."; } else { $name = test_input($_POST["name"]); $error = false; } if ( !filter_var(($_POST["email"]),FILTER_VALIDATE_EMAIL) ) { $error = true; $emailErr = "Please enter valid email address."; } else { $email = test_input($_POST["email"]); $error = false; } if (empty($_POST["comment"])) { $comment = ""; $commentErr = ""; $errorcomment = true; } else if (strlen($comment) < 3) { $errorcomment = true; $commentErr = "Name must have at least 3 characters."; } else if (strlen($comment) > 2200) { $commentErr= "You have a limit of 2200 characters."; $errorcomment = true; } else { $comment = test_input($_POST["comment"]); $errorcomment = false; } if (empty($_POST["tel"])) { $tel = ""; $telErr = ""; $errortel = true; } else if (strlen($tel) < 3) { $errortel = true; $telErr = "Name must have at least 3 characters."; } else if (strlen($tel) > 25) { $telErr= "You have a limit of 25 characters."; $errortel = true; } else { $tel = test_input($_POST["tel"]); $errortel = false; } /*session_start(); $token = $_SESSION['token']; echo $token; echo "<br />"; echo $_SESSION['token']; echo "<br />"; echo $_POST['token']; echo "<br />"; $compToken = $_POST['token']; echo $compToken; if($compToken !== $token) { // It's SPAM $spamErr = "This is spam"; $errorspam = true; } else{ $errorspam = false; } */ } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> </body> </html> |
| extra.php Script that produces token
in the CSRF token
|
|
<?php session_start(); if(empty($_SESSION['token'])) { // RANDOM STRING CREDITS : https://www.thecodedeveloper.com/generate-random-alphanumeric-string-with-php/ $length = 32; $_SESSION['token'] = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $length); // FOR PHP 7, CREDITS : https://stackoverflow.com/questions/6287903/how-to-properly-add-csrf-token-using-php // $_SESSION['token'] = bin2hex(random_bytes(32)); return true; } else{ return false; } ?> |
| send.php A script to send the email
If the header is not valid (malicious code as been inserted) diplay error You will need to alter the line $success = mail($EmailTo, $Subject, $Body, "From: <$>"); Change EmailTo to where you require the formail to send to and also EmailFrom will be where you which to show you have sent the email from - normally your the email where the formmail is hosted |
|
// validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; exit; } $body = " Name: $name Tel: $tel E-mail:$email Message: $comment "; // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $name; $Body .= "\n"; $Body .= "Tel: "; $Body .= $tel; $Body .= "\n"; $Body .= "Email: "; $Body .= $email; $Body .= "\n"; $Body .= "Message: "; $Body .= $comment; $Body .= "\n"; if (!empty($name) && !empty($email) && !empty($comment) && empty($error) && empty($errorname) && empty($errorcomment ) && empty($errorspam) && empty($errorcaptcha)) { $successErr = "<h2 style=color:rgb(0,255,0);>Thank you for contacting Us</h1> <h1 style=color:rgb(0,255,0);>We value yor feedback</h2> "; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); $name = $email = $tel = $body = $comment = ""; } else { $all = "<h2 style=color:rgb(255,5,0); >Contact US</h2> \n"; } ?> |
| captcha.php This is ready written code to
use google captcha
You will nedd a google account and be able to get API keys |
|
/* Author: Joshua Herbison of Idea Pro LLC, IdeaPro.com */ /* The code below is only for an example and testing purposes.*/ /* With API keys, this is a fully functional and working form, but it does not send the information anywhere. This code is "Use at your own risk". Idea Pro LLC and/or Joshua Herbison is not responsible for any developer's misuse of any code we provide in Tutorials. By using this code you agree to those terms.*/ $public_key = "place your public key here gu8ZgKy6ZVgtQFkOrXud6z"; /* Your reCaptcha public key */ $private_key = "place your private key here CKL1SJFpZffWyAbHGe"; /* Enter your reCaptcha private key */ $url = "https://www.google.com/recaptcha/api/siteverify"; /* Default end-point, please verify this before using it */ /* Check if the form has been submitted */ if(array_key_exists('submit',$_POST)) { /* The response given by the form being submitted */ $response_key = $_POST['g-recaptcha-response']; /* Send the data to the API for a response */ $response = file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']); /* json decode the response to an object */ $response = json_decode($response); /* if success */ if($response->success == 1) { echo "You passed validation!"; } else { $errorcaptcha = true; echo "You are a robot and we don't like robots."; } } |