Forum Moderators: coopster
I have been slowly learning PHP, and improving my codes on my site each week. One of those things have been my web forms. One of the latest features I have incorporated is a simple image verification script in my contact form, and now I get ZERO spam, where as before I was getting hundreds of spam form mails each day!
I was wondering if there was any other security codes to put into my form to protect it from being used to send out emails from, etc...Here is my PHP code so far:
<?php
if ($_POST["image"] == "26mush3") {
if ($_SERVER['REQUEST_METHOD']=="POST"){
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ¦¦
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Values submitted by the user:\n";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}
$recipient=($_POST["recipient"]);
$subject="Example.com Contact Form";
error_reporting(0);
if (mail($recipient, $subject, $msg, "", "-f christian@example.com")){
echo "<center><h1>Thank You</h1><p>Message successfully sent! <a href=http://www.example.com>Click here</a> to return to the Example.com home page.</p></center><br>\n";
echo nl2br($input);
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
} else {
echo "<center>You did not enter the proper image verification code. Please hit your back button and try again.</center>";
}
?>
As you can see, it is pretty simple, but so far it has been working great!
Thanks,
Christian
[edited by: dreamcatcher at 7:26 am (utc) on Aug. 31, 2007]
[edit reason] Use example.com, thanks. [/edit]
You want to verify that the recipient is valid. If you have specific recipients in the drop down menu, then checking the recipient posted against one of the approved emails in the drop down menu should do it.
man..beat me to it. Oh well, I was more detailed. :)
[edited by: Philosopher at 3:10 pm (utc) on Aug. 31, 2007]
So basically, how it is written now, if a spammer wanted to use the form to spam other people, they couldn't, because the recipient would have to match one of the emails in my array? Thanks again!
<?php
$emails = array("email1", "email2", "email3", "email4", "email5", "email6", "email7", "email8", "email9");
if(in_array($_POST['recipient'], $emails)){ //in_array() is case sensitive
//mail function goes here
if ($_POST["image"] == "26mush3") {
if ($_SERVER['REQUEST_METHOD']=="POST"){
// In testing, if you get an Bad referer error
// comment out or remove the next three lines
if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ¦¦
!strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
die("Bad referer");
$msg="Values submitted by the user:\n";
foreach($_POST as $key => $val){
if (is_array($val)){
$msg.="Item: $key\n";
foreach($val as $v){
$v = stripslashes($v);
$msg.=" $v\n";
}
} else {
$val = stripslashes($val);
$msg.="$key: $val\n";
}
}
$recipient=($_POST["recipient"]);
$subject="example.com Contact Form";
error_reporting(0);
if (mail($recipient, $subject, $msg, "", "-f christian@example.com")){
echo "<center><h1>Thank You</h1><p>Message successfully sent! <a href=http://www.example.com>Click here</a> to return to the example.com home page.</p></center><br>\n";
echo nl2br($input);
} else
echo "An error occurred and the message could not be sent.";
} else
echo "Bad request method";
} else {
echo "<center>You did not enter the proper image verification code. Please hit your back button and try again.</center>";
}
} else {
echo "<center>Bad request method.</center>";
}
?>
[edited by: eelixduppy at 7:04 pm (utc) on Aug. 31, 2007]
[edit reason] please use example.com [/edit]