TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-29-2009, 11:41 AM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default Email script problem

I have had a script running that once a user enters their information it will email it to me. However, if they hit refresh it sends it again. How would I go about preventing this from happening?

Thanks for any help.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 05-29-2009, 12:34 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by code_junkie View Post
I have had a script running that once a user enters their information it will email it to me. However, if they hit refresh it sends it again. How would I go about preventing this from happening?

Thanks for any help.
How about you show us some code?
allworknoplay is offline  
Reply With Quote
Old 05-29-2009, 12:42 PM   #3 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Its just the basic mailto function.
Code:
$to = "email@address.com";
$from = "email@address.com";
$subject = "Message from site";
$message = "";
$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

ini_set('sendmail_from', $from);
mail($to, $subject, $message, $headers);
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 05-29-2009, 02:06 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by code_junkie View Post
Its just the basic mailto function.
Code:
$to = "email@address.com";
$from = "email@address.com";
$subject = "Message from site";
$message = "";
$headers  = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";

ini_set('sendmail_from', $from);
mail($to, $subject, $message, $headers);
Ok, well based on that, of course it's going to run on every refresh because there's nothing stopping it from running.

If you include an IF conditional that will check for a specific status, it will only run once..

I like to base it off a hidden POST variable say, "submit_form" and set that value to true. Then on the receiving page, make sure that it is set to true, then run your mail function.

After your mail function, unset that variable, that way it won't be true anymore and then if they hit refresh, it won't send the email.
allworknoplay is offline  
Reply With Quote
The Following User Says Thank You to allworknoplay For This Useful Post:
code_junkie (05-29-2009)
Old 05-29-2009, 02:12 PM   #5 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Thanks for your response. I'm not very familiar with PHP yet, so can show me some example code or a site so I can reference from? I need to see it to figure it out and inderstand it.
Thanks
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 05-29-2009, 07:48 PM   #6 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

PHP/ELSE
PHP/EXIT
PHP/MAIL
PHP/IF

this should be a good start.

PS: the best way to go is to read all of php.net manual one or two times..

PHP Manual
codefreek is offline  
Reply With Quote
The Following User Says Thank You to codefreek For This Useful Post:
code_junkie (05-29-2009)
Old 05-29-2009, 08:33 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Have the page that actually sends the page redirect, the HTML code for this is (put this in the <head>):
Code:
<meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com"></HEAD>
This will redirect the page away immediately after the message is sent.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-31-2009, 09:09 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

The header function will also redirect using GET and therefore prevent any reloading.

php Code:
header('location: FQDN');
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-01-2009, 02:18 PM   #9 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Now to make things more complicated

You could also take the IP of the user, log it, and check whether he already send you a message in - lets say last hour. If yes, then he's not allowed to send more messages. Using this way you could also make sure that your Mailbox does not get spammed from one user.

-> There are many people out there who just love writting such little programms.
Sakakuchi is offline  
Reply With Quote
Old 06-01-2009, 03:05 PM   #10 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

I think I should have explained the whole thing. This site is an incident reporting site.

Page 1:
  • user enters data
  • on submit it redirects to page 2 (url containing incident number)

Page 2:
  • emails webmaster informing them there has been an incident
  • shows user there incident
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 06-01-2009, 04:40 PM   #11 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Sakakuchi View Post
You could also take the IP of the user, log it, and check whether he already send you a message in - lets say last hour. If yes, then he's not allowed to send more messages. Using this way you could also make sure that your Mailbox does not get spammed from one user.
That way, ALL users who share an IP address will be blocked in that interval and will not be allowed to send any message, which obviously is not what is wanted.

You could do this a couple of ways:

1. the most simple method -> redirect the user to another page after he/she has submitted the form (after you send the e-mail)

2. use a 'security key' (this is what I call it). What does this mean? Each time you show the user the page, you also generate a hash key (an md5 of variable data), which you will store in the session. Then, when the user hits the submit button and after you validate your form, etc., you check that session variable against what you would expect. If it's different, you can reset the form values and present the user with a message saying "please use the submit button" or such. Something like this:

Code:
if(form_submitted)
{
    // validate your form, etc. and continue only if all fields are correctly completed

    // $_POST['security_key'] is an input in the form (a hidden input), in which you echo your $_SESSION['security_key'] - so you could compare them later
    if($_SESSION['security_key'] != $_POST['security_key']) {
        // show a message or what ever. validation failed, so you will not send the e-mail yet
    } else {
        // send your e-mail, show the user the "thank you" page, etc.
    }

    // invalidate the security token with each new request
    $_SESSION['security_key'] = regenerate_security_key();
}
else
{
    // form has not been submitted, so keep generating keys
    $_SESSION['security_key'] = regenerate_security_key();
}
3. using a cookie. However, this is NOT a reliable technique, as the user's browser might block these cookies, or even the user might delete them.

So, the best choice would be #2.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by xenon : 06-02-2009 at 10:55 PM.
xenon is offline  
Reply With Quote
Old 06-01-2009, 04:54 PM   #12 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

I'll provide some simple code later today to help you on your way...
allworknoplay is offline  
Reply With Quote
Old 06-01-2009, 05:59 PM   #13 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
That way, ALL users who share an IP address will be blocked in that interval and will not be allowed to send any message, which obviously is not what is wanted.

Hmm true - didn't think of that. Considering that I would also choose the Session - Solution.
Sakakuchi is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Script to Extract Email Address from any text sunilbhatia79 Tips & Tricks 5 06-11-2009 02:06 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
Email Verification Class Alan @ CIT Script Giveaway 1 02-09-2008 08:58 PM
PHP Script to Extract Email Address from any text sunilbhatia79 Show Off 5 11-15-2007 01:53 PM


All times are GMT. The time now is 01:50 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design