Source Rally PHP Community Scripts .. Sign up .. Login
Recently I have noticed that once somebody sends you a message with your contact form they proceed to send you several variations of the same message one right after another.
Access: Public      Tags: Contact Form, Cookies, Single Submit, PHP, HTML
Add to favourites       Subscribe comments       Copy code       Bookmark
Recently I have noticed that once somebody sends you a message with your contact form they proceed to send you several variations of the same message one right after another. The following is a contact form that places a cookie on the user and doesn’t allow them to submit a new message until the browser is closed.

    <?php
    $email 
"your@email-here.com";

    
$formhide 0;
    if (
$_COOKIE[c_sent]) {
        echo 
"Your message has been sent.";
        
$formhide 1;
    }
    if (isset(
$_POST[trigger])) {
        if (
$_POST[email] && $_POST[subject] && $_POST[body] && $_POST[name]) {
            
setcookie(’c_sent’10"/");
            
mail($email"Contact Form - ".$_POST[subject], $_POST[body], "From: ".$_POST[name]." <".$_POST[email].">");
            echo 
"Your message has been sent.";
            
$formhide 1;    
        }
        else {
            echo 
"You must complete all feilds.";
        }
    }
    if (!
$formhide) {
    
?>
    <form method="post">
        <table id="contact" cellpadding="0" cellspacing="0">
            <th colspan="2">  Contact</th>
            <tr><td colspan="2"> </td></tr>
            <tr>
                <td valign="top">Name:</td>
                <td valign="top"><input type="text" name="name" value="<? echo $_POST[name]; ?>" /></td>
            </tr>
            <tr>
                <td valign="top">Email:</td>
                <td valign="top"><input type="text" name="email" value="<? echo $_POST[email]; ?>" /></td>
            </tr>
            <tr>
                <td valign="top">Subject:</td>
                <td valign="top"><input type="text" name="subject" value="<? echo $_POST[subject]; ?>" /></td>
            </tr>
            <tr>
                <td valign="top">Body:</td>
                <td valign="top"><textarea name="body"><? echo $_POST[body]; ?></textarea></td>
            </tr>
            <tr>
                <td colspan="2" class="submit"><input type="submit" name="submit" value="Submit" /></td>
            </tr>
        </table>
        <input type="hidden" name="trigger" />
    </form>
    <?php ?>
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Comments

Iammabbella - 18-01-2007 10:57
Add to friends       Mail user
Hello coder!
I have test your script, and i have found some bugs...
use if (IsSet($_COOKIE['c_sent'])) {
instead, and the singles quotes in ['c_sent'].
And then redirect the user to another page, it's much better.
feilds it's fields.
Ok, now you kill me.. :)
Thank you for your patience...
Bye
regin - 12-01-2007 23:53
Add to friends       Mail user
Hey nexus - following the advice on www.sourcerally.net/regin/8-The-PHP-coder%27s-top-10-mistakes-and-problems you should change:
<? echo $_POST[body]; ?>

T0:
<? echo htmlspecialchars($_POST['body'],ENT_QOUTES); ?>

A good advice: make it a habbit when printing/echoing variables that are not supposed to contain html.
More comments: 1
Shared by:

nexuslite

Mail user Add to friends
All user contributed content is available under the LGPL unless specified otherwise.
Remaining copyrights Regin Gaarsmand © 2006-2008
About SourceRally.net
Programador PHP