PHP Contact Form Script

For some odd reason my “Contact Me” form disappeared from the Ogrehut’s web server. It was a cut and paste form and was pretty ugly but it worked. I use a contact form rather than a mailto: link because spammers glom onto mailto: links on websites. A contact form allows people to contact me while keeping me secure from spammers so I needed to replace the form.

So the quest for a replacement begins… a little scrounging around on the ‘net and a nearly identical script was discovered. The nice thing about this script is that it sends the email with PHP so there it completely hides the destination email account from public view. Additionally it includes in email the page which the web site viewer was on, information about their browser, and the end user’s IP address. However this script is ugly and it had some major usability warts which needed to be worked on. So the decision was made to give the contact form would be need a code makeover.

After a bit of code bending the resulting revised code is a big improvement not just in appearence but in functionality. The original form used two PHP pages and if there was a error you had to page back to fix it. Additionally after sending the email you are left in the form with nowhere to go. Even worse it could easily be attacked by spammers using cross site scripting. To address these issues the script was significantly revised.

Here’s the code for the revised form.

First the two pages were merged into one, this allows it to do error checking and allowed a simple spam prevention measure to be added. When the form is requested it checks for a key value. If the key is absent that means either it’s the first time the form was displayed (or that it was submitted by a spammer) and it shows the form. If the key is present then it validates the information on the form. If the form is invalid the form is redisplayed with an error message to help the user. If the form is valid then a Thank You message is displayed and the message is sent.

The other major change was the use of the http referrer information to retain the page which the user originated the request. The referring page is linked at the bottom of both the form and the Thank You page so the user can return to where they were at any time.

Finally there was some simple beautification done to the form. All the beautification is in the form of some simple CSS which is included in the page so the form stays completely self contained.

If you would like to use this form on a site download it, rename it to something.php, and edit the email address in the form to point to the target email account. There are a few other things you can easily customize in the form as well. Or you can use your PHP kung foo and lobotomize the script if you’d like.

5 thoughts on “PHP Contact Form Script”

  1. Hey thanks for the code. I have often thought about addind a contact page to my site but never really persued it. I just so happen to bump into your code here and decided since it was right in front of me to take advantage of it. You can check it out on my site at: Thanks a lot!

  2. Simple, Elegant script. Did the job for me. I did add phone # and removed referral link.

Comments are closed.