Contact.php

I am using a contact.php i downloaded for for my website and its all working fine but the only problem I’m having, is that every time my webpage gets reloaded or someone loads it, I get sent a blank email. Is there anything wrong with the .php? any help would be great I’m pretty new to all this. This is my .php

Then, on your contact.php script, check that the token exists and is valid:

< ?php
session_start();
if(
// if there’s a token in the session
! empty( $_SESSION )
// AND a token in the form submission
&& ! empty( $_POST )
// AND the tokens match
&& $_SESSION === $_POST
){
// THEN, the form submission is legit.

// first, DELETE the token from the session:
unset( $_SESSION );
// that way, if the user hits the [back] button,
// the script will ignore the repeated submission
// because there’s no matching token in the session.

// next, proceed with processing the form submission
// and sending the email as normal.
}else{

// if there’s no matching token,
// the form submission is a duplicate
// (or possibly from a really old visit, and the session has expired).
// so, don’t process it or send any emails.

// you might redirect to the contact form again,
// or the homepage, or whatever you like.
}

*****
**Edit**

> there doesn’t seem to be any actionable item aka: a submit button.

since the entire `

` is missing from the code sample, I’m assuming that it’s on another page and functioning properly. He mentioned that the problem occurred when someone *reloaded* the page (or, presumably, navigated to it via the [back] button or by accident). @markblackler, does the script email you successfully when you submit the form the first time?

> The biggest problem lies in your $mail_status, currently it just checks if it exists / has a value which can include blank values.

`$mail_status` comes from the call to `mail()`, which will always be either `true` or `false`. It should work as expected.

i tired what you said, but what i did, didn’t resolve the problem, i might of done it wrong as i wasn’t to sure how to do it cause i have never used tokens before, i will give it another go and see what happens

yes! that worked I’m not getting spammed with emails now, thank you so much for your help, I really do appreciate it, what I was doing wrong, was, I tried to attach another .php file instead of changing my code in my html file, sounds pretty stupid when I’m typing it now.

Tips

What now? I have some ideas for you.

Go explore CodePen!

As a front end designer and developer, you should have an account on CodePen so you can save your snippets, present your ideas, and engage with other front end folk. I'd encourage you to go PRO as well, to unlock the full power of CodePen.

Get the newsletter!

You should sign up for the CSS-Tricks newsletter. It's a clean copy of all the blog posts each week, combined together, right to your inbox. If email isn't your thing, there is an RSS feed, iTunes, and lots of other ways to subscribe.

Listen to ShopTalk!

Subscribe to The Lodge!

The Lodge is a members-only, ad-free video learning area here on CSS-Tricks. Just like the free screencasts, but organized into four large complete series. Membership is also the #1 best way to support CSS-Tricks.

We can do the real footer now.

Site Links

Colophon

CSS-Tricks* is created, written by, and maintained by Chris Coyier. It is built on WordPress, hosted by Media Temple, and the assets are served by MaxCDN. The fonts are Source Sans and Source Code Pro. It is made possible by viewers like you who subscribe to The Lodge and through advertising for products and services I like.