Contact Form - Delayed timing?
#1
Posted 09 November 2006 - 12:33 PM
On my website I have a contact form that user's can fill out and send to my email address. A friend pointed out to me today (very annoyingly) that people can fill out the form, send it, click on the back button and then resend it, doing this over and over again, basically spamming my inbox.
Basically what I want to know is, is there a piece of code which I can add to my PHP denying users to contact me for say 5 minutes, after they have sent one message?
Thanks a bunch
Mark
#2
Posted 09 November 2006 - 01:02 PM
You could throw their IP into a database with a timestamp, and check to see if that IP has sent an e-mail within the last $x seconds. But again, that's not fool proof either.
#3
Posted 09 November 2006 - 01:44 PM
#4
Posted 09 November 2006 - 05:45 PM
Would anyone be able to supply the code for the cookie part? and How to do it, its just I don't know or understand how to do it. Thanks very much guys.
I already have the php file for the submission of the form done, so would I add the coding to that?
#5
Posted 09 November 2006 - 10:13 PM
<?php
if(!$_COOKIE['contacted']){
setcookie('contacted', true, time()+(60*5), '/');
//
/* Process and send email here */
//
}
else{
//
/* Echo error or redirect, whatever you wish */
//
}
?>
#6
Posted 16 November 2006 - 02:17 PM
What I want to know, is whats wrong with this code? Line 47 comes up with an error, and users can still use the contact form within seconds.
Thanks for your helps guys and girls, I really do appreciate it.
<?php
$subject = 'Results from Contact form';
$emailadd = 'Mark@playawayuk.com';
$url = 'contactsent.html';
$req = '0';
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
$ip = $REMOTE_ADDR;
if($sentmessage == '1'){
echo "You have already sent a message!";
exit();
}else{
$num = $sentmessage + 1;
//This is line 47 below
setcookie("sentmessage","$num",time()+600);
}
?>
#7
Posted 16 November 2006 - 02:34 PM
and
$sentmessage = $_COOKIE['$sentmessage'];
Thats after a quick look over
Matt
#8
Posted 16 November 2006 - 02:58 PM
I will have to read into cookies.
#9
Posted 16 November 2006 - 05:11 PM
Another problem though.
I've only just realised that there is one piece of information I don't receive from my contact form.
On my contact form I have a drop down menu, which the user can chose the subject type, i.e Feedback, Complaint, Picture Contribution ETC.
How do I go about adding this information to the PHP script?
#10
Posted 16 November 2006 - 07:21 PM
Show us the html part
#11
Posted 16 November 2006 - 10:39 PM
HTML
<select name="subject"> <option value="feedback">Feedback</option> <option value="complaint">Complaint</option> <!-- So on and so forth... --> </select>
PHP
$subject = trim(html_special_chars($_POST['subject']));
Remember it will grab what is in the 'value' attribute, and not what is between the tags, and be sure to validate it before using it for anything. Never trust user submission, no matter what form element it is from...
#12
Posted 17 November 2006 - 09:48 AM
any way, thanks again guys.
#13
Posted 21 November 2006 - 11:55 AM
I've tried to do this cookie thing but I just can't get to grips with it. If anyone has the time, then could you possibly help me with this?
If I paste the code I am using (I've changed the code from the original one, as I wanted to do it myself using the tutorials) then would you be able to enter the cookie code? Then I will be able to understand what's going on. You will be acknowledged for your help on the website. Thank so much!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$team = $_POST['team'];
$comments = $_POST['comments'];
$ip = $_SERVER['REMOTE_ADDR'];
$recipient = "Mark@playawayuk.com";
$emailsubject = "Email sent from Contacts Page";
$message = "E-mail: $email \n";
$message .= "Subject: $subject \n";
$message .= "Supports: $team \n";
$message .= "Message: $comments \n";
$message .= "IP Address: $ip";
$headers = "From: $name \n";
$headers .= "Reply-To: $email";
mail($recipient,$emailsubject,$message,$headers);
$URL="contactsent.html";
header ("Location: $URL");
?>
#14
Posted 21 November 2006 - 05:12 PM
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$team = $_POST['team'];
$comments = $_POST['comments'];
$ip = $_SERVER['REMOTE_ADDR'];
$recipient = "Mark@playawayuk.com";
$emailsubject = "EmailsentfromContactsPage";
$message = "E-mail:$email\n";
$message .= "Subject:$subject\n";
$message .= "Supports:$team\n";
$message .= "Message:$comments\n";
$message .= "IPAddress:$ip";
$headers = "From:$name\n";
$headers .= "Reply-To:$email";
if(!isset($_COOKIE[delay])){
mail($recipient, $emailsubject, $message, $headers);
// sets cookie called 'delay', value doesn't matter as it only checks if the cookie is there, cookie will be trashed after 300 second, 5 minutes.
setcookie('delay', 'null', time()+60*5);
}
$URL = "contactsent.html";
header("Location: $URL");
?>
#15
Posted 21 November 2006 - 05:17 PM
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$team = $_POST['team'];
$comments = $_POST['comments'];
$ip = $_SERVER['REMOTE_ADDR'];
$recipient = "Mark@playawayuk.com";
$emailsubject = "Email sent from Contacts Page";
$message = "E-mail: $email \n";
$message .= "Subject: $subject \n";
$message .= "Supports: $team \n";
$message .= "Message: $comments \n";
$message .= "IP Address: $ip";
$headers = "From: $name \n";
$headers .= "Reply-To: $email";
if($_COOKIE['sentmessage']!=1){
mail($recipient,$emailsubject,$message,$headers);
$URL="contactsent.html";
header ("Location: $URL");
setcookie("sentmessage",1,time()+600);
}else{
echo "You've allready sent a message";
}
?>
Should work, not tested though
Edited by dotbart, 21 November 2006 - 05:19 PM.
#16
Posted 21 November 2006 - 05:38 PM
I know its only basic stuff, but that's the level im at currently.
But I've managed to enter and change some coding to the php code. I've managed to put a validation in, and also changed it so that instead of getting a page saying "You've already sent a message" it now redirects to a page I created.
Thanks guys and girls, your really helping me with learning php.
Edited by Tarquin, 21 November 2006 - 05:46 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
