Tricks to a Simple and Secure Contact Form using PHP

Jump To:   «  1 2 3  View All »

A day does not go by it seems that someone is asking how to create a simple contact form in PHP. The problem I tend to find is that most of the tutorials online show bad programming practice, such as using the @ error suppressor or not validating / checking the data passed into the form. I decided to write up a few of my tricks on how to do a proper PHP Secure Contact form and attempt to explain why. As always, I welcome comments / suggestions in the remarks. I have decided to omit the Captcha, as I feel Captcha is just broken, so if you want to prevent spam, you will need to research other methods, like Honey Pots. Let’s get this tutorial rolling!

First, we will just start with a simple form, you do not have to use the form, just remember that you will need to keep / change the variable names to suit your needs. For this tutorial, we will ask for the User’s name, Email and a contact message, simple.

    <title>Slunked Simple and Secure Contact Form</title>
    <form action="mail.php" method="post" name="contactform">
        Username: <input type="text" value="" name="username" /><br />
        Email: <input type="text" value="" name="email" /><br />
        Comments: <textarea name="comments"></textarea><br /><br />
        <input type="submit" name="submit" value="Submit" />

Like I said, just a simple basic form, hopefully you understand a bit of HTML to know what is going on there. Now that we have the simple HTML Form, it is time to show you my tricks on the PHP side of the code.

Jump To:   «  1 2 3  View All »


Leave us a comment

  • eagle eyes

    rule number one of php… close your tags. ie. the most important one… ?>

    • Jim

      Sorry eagle eyes, not sure who taught you php, but the ?> closing tag is not important by any means. PHP will automatically close it and using the ?> is prone to cause issues, such as with includes and header errors.

      Glad to have taught you something!

Leave a Comment