Add a captcha. Request for a security code, then match the code.
gdimage.php - this will create 4 image codes. you can amend to have different length
in $random_text = substr(md5(uniqid()),0,4);
PHP Code:
<?php
session_start();
header("Content-type: image/png");
$im = @imagecreate(80, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
$random_text = substr(md5(uniqid()),0,4);
imagestring($im, 5, 5, 5, $random_text, $text_color);
imagepng($im);
$_SESSION['verify'] = $random_text;
?>
contact.php -- uses session to remember random text generated by gdimage.
PHP Code:
<?php
session_start();
define('RECIPIENTEMAIL','test@gmail.com'); // define your email (Recipient) here.
define('SENDEREMAIL','NoReply@gmail.com'); // define Senderemail here (Shows as Sender's email)
if (isset($_POST['submit'])){ // check if submit button is clicked
if ($_SESSION['verify'] == $_POST['security_code']){
$name = 'From: '.$_POST['name']."\r\n"; // name valaue in email body as first line.
$email = 'Email: '.$_POST['email']."\r\n"; // email value in email body as second line.
$body = $name.$email.$_POST['message']; // body first line, second line plus message
$subject = 'Web Message'; // shows as subject in email
$to = RECIPIENTEMAIL;
$header = 'From: '.SENDEREMAIL."\r\n";
if (@mail($to, $subject, $body, $header)){ // PHP mail function to send email.
echo '<b>Message send succeed</b><br />';
unset( $_SESSION['verify'] );
}else{
echo '<b>Message send failed</b><br />';
unset( $_SESSION['verify'] );
}
}else{ // if not sercity code
echo '<font color="red"> Sorry, email can not continue..<br />
You have provided an invalid security code</font><br />';
}
}
// a form to collect user name, email and message
echo '<table><form action="'.$_SERVER['PHP_SELF'].'" method="post">
<tr><td><label>Name</label></td><td><input type="text" name="name"></td></tr>
<tr><td><label>Email</label></td><td><input type="text" name="email"></td></tr>
</table>';
echo '<table>
<tr><td><label>Message</label></tr>
<tr><td><textarea name="message" cols=50 rows=8></textarea></td></tr>
</table>
<table>
<tr><td>Security Code</td><td><img src="gdimage.php" /></td></tr>
<tr><td>Enter Security Code</td><td><input name="security_code" type="text" size="8" />
<tr><td align="right"><input type="submit" name="submit" value="Send Message"></td></tr></table>
</form>';
?>