Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2013, 04:30 AM   PM User | #1
jack0987
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
jack0987 is an unknown quantity at this point
Form to email help

I am not a php person and need some help, please.

I have the following rudementary form to email code.
(I did not write this code.)
It is in a file named mail.php on the server.

PHP Code:
<?PHP 

$to 
"me@this.com";

$subject "Results from your Request Info form"

$headers "From: My Site"

// redirect? 1=yes 0=no
$forward 0
$location "mysite.com"

$date date ("l, F jS, Y"); 
$time date ("h:i A"); 

$msg "Below is the result of your feedback form. It was submitted on $date at time.\n\n"

if (
$_SERVER['REQUEST_METHOD'] == "POST") {
    foreach (
$_POST as $key => $value) { 
        
$msg .= ucfirst ($key) ." : "$value "\n"
    }
}
else {
    foreach (
$_GET as $key => $value) { 
        
$msg .= ucfirst ($key) ." : "$value "\n"
    }
}

mail($to$subject$msg$headers); 
if (
$forward == 1) { 
    
header ("Location:$location"); 

else { 
    echo 
"Thank you for submitting our form. We will get back to you as soon as possible."


?>

The following is a typical response:

Below is the result of your feedback form.
It was submitted on Friday, February 1st, 2013 at 03:30 PM.

name : test
Phone : asdf
Email : sdfg@sdfg.com
Submit : Submit Request


THe code works fine when called from the web page with the form except every now and then I get an email without the fields such as this:

Below is the result of your feedback form.
It was submitted on Friday, February 1st, 2013 at 05:30 PM.


I discovered that if you access the mail.php file directly by typing

http://mysite.com/mail.php

in your browser you get an email without the fields.


Is there a better way of doing this?

Any comments would be helpful. Thanks.
jack0987 is offline   Reply With Quote
Old 02-02-2013, 09:36 AM   PM User | #2
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
Start here:
http://www.codingforums.com/showthre...68#post1144368
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:
student101 is offline   Reply With Quote
Users who have thanked student101 for this post:
jack0987 (02-02-2013)
Old 02-02-2013, 05:18 PM   PM User | #3
elem
New Coder

 
Join Date: Sep 2011
Posts: 35
Thanks: 18
Thanked 1 Time in 1 Post
elem is an unknown quantity at this point
if all you want is to prevent users from accessing the mail.php themselves then just add
PHP Code:
if(isset($_POST)) {

//your content

}

else {

echo 
"Forbidden";
//or
header('Location: index.php'); //which will bring them back to home page


elem is offline   Reply With Quote
Users who have thanked elem for this post:
jack0987 (02-02-2013)
Old 02-02-2013, 06:09 PM   PM User | #4
jack0987
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
jack0987 is an unknown quantity at this point
Thanks so much for the replies.

Actually elem I did do something very similar to that yesterday.

I have a suspicion that it is being done intentionally. For what it's worth, perhaps I could catch the http line or ip address but I do not know how to do that.
jack0987 is offline   Reply With Quote
Old 02-02-2013, 10:02 PM   PM User | #5
elem
New Coder

 
Join Date: Sep 2011
Posts: 35
Thanks: 18
Thanked 1 Time in 1 Post
elem is an unknown quantity at this point
Do you mean that someone is intentionally pressing send without filling out any fields? Then you could write a few lines that would check if all the fields are filled out, if not form wouldn't be sent. Or install captcha or some other anti-spam script, if you suspect it is not a human ...
elem is offline   Reply With Quote
Old 02-02-2013, 10:17 PM   PM User | #6
jack0987
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
jack0987 is an unknown quantity at this point
No, I think someone may be running the mail.php directly. As far as I can tell it is the only way the email would be totally blank - have no field names at all. If they were hitting submit the field names would be present but empty.
jack0987 is offline   Reply With Quote
Old 02-03-2013, 08:58 AM   PM User | #7
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
Quote:
Originally Posted by jack0987 View Post
No, I think someone may be running the mail.php directly. As far as I can tell it is the only way the email would be totally blank - have no field names at all. If they were hitting submit the field names would be present but empty.
Edit: reference

You still getting blank emails after applying the above?
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:
student101 is offline   Reply With Quote
Old 02-03-2013, 02:51 PM   PM User | #8
jack0987
New to the CF scene

 
Join Date: Nov 2012
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
jack0987 is an unknown quantity at this point
No. The above application corrects it. Thanks.

I think I may still like to catch the IP and http line that runs it perhaps.

Last edited by jack0987; 02-03-2013 at 02:59 PM..
jack0987 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:15 PM.


Advertisement
Log in to turn off these ads.