FormMail security

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aidan

    #1

    FormMail security

    Hi PHP guru's,

    I've been working on creating a PHP formmail script. I have a working
    version, but I'd like to get feed back on what security holes I may have
    opened, and what I could do better. Here's the code:

    <?php
    // $to - set this to where form contents should be sent
    $to = 'someone@somewh ere.com';

    // $subject - the subject of the message to send to $to
    $subject = 'Yay FormMail!';

    // $from - who the email should appear to be from
    $from = 'formmail@examp le.com';

    // $thanks_page - URL of page to redirect to when the mail is sent
    successfully
    $thanks_page = 'http://www.example.com/thanks.html';

    // $error_page - URL of page to redirect to when there is an error
    $error_page = 'http://www.example.com/error.html';

    // $allowed_refere rs - comma separated list of hostnames where form
    contents can originate.
    // POST's or GET's comming from anywhere else will be rejected.
    $allowed_refere rs = 'example.com,ww w.example.com';

    /*//////////////////////////
    // DONT EDIT BELOW HERE!!!//
    //////////////////////////*/

    //Check that the referer is valid
    $referers = explode(',', $allowed_refere rs);
    preg_match('/http*\:\/\/(.*)\/.*/', $_SERVER[HTTP_REFERER], $matches);
    $referer = $matches[1];
    // if not, redirect to $error_page
    if(!array_searc h($referer, $referers)) {
    header("Locatio n: $error_page");
    }

    // Check which method was used to send data, and sanitise it
    if(count($_POST ) > 0 || count($_GET) > 0) {
    if(count($_POST ) > 0) {
    foreach($_POST as $k => $v) {
    $form[strval($k)] = strip_tags(strv al($v));
    }
    } else {
    foreach($_GET as $k => $v) {
    $form[strval($k)] = strip_tags(strv al($v));
    }
    }
    } else {
    header("Locatio n: $error_page");
    exit();
    }

    $message = "Form submitted from $_SERVER[HTTP_REFERER] at " . date('h:ia D
    jS F Y') . "\n\n";

    // Convert the form data from an array into a string, ready for sending
    foreach($form as $k => $v) {
    $message .= "$k\t==>\t$v\n" ;
    }

    if(mail($to, $subject, $message, "From: $from")) {
    header("Locatio n: $thanks_page");
    } else {
    header("Locatio n: $error_page");
    }

    ?>

    What do you think? Thanks in advance...

    Regards,

    Aidan


Working...