Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
HTML form to Email using PHP - Help a newbie pleasie!
Old 05-12-2008, 04:24 PM HTML form to Email using PHP - Help a newbie pleasie!
Junior Talker

Posts: 2
Name: John
Location: North Wales
Trades: 0
Hi,

After having my contact form hacked to send spam I have had a go and tried to write a php script using bits from different scripts etc. Just wondered if some of you more experienced with PHP (I've only been learning as I go)would take a look at it for me and tell me what you think please?

On the HTML form side I used Javascript to make sure certain fields are filled in and a valid email address is entered before passing the info over to my php script. On the HTML form page I have a hidden field (using CSS Display none) and if a bot fills this field in then the PHP script is set to recognise it as spam and it will not be sent(will this work?)

The error page you get directed to just basically says click back on your browser and make sure you have entered your name, email and enquiry details (only using the error page incase people have Javascript switched off)

I've tested it locally and online (a spare domain I have), and everything seems to work (not sure how I check to see if the code stopping new lines works, any ideas?) - is there anything I need to add to the script? (I am planning on adding a Captcha to my form, but I thought I would make a start on the script first)

Thanks
M

PHP Code:
<?php

$spam 
$_POST['info'];

// Check whether hidden field has been filled
if ($spam != "") {
// if so print error message and exit
echo "Suspected injection attempt - mail not being sent."; exit;
} else

// check to see whether name, enquiry details and email contain info in case javascript is off
if (empty($_POST['name']) || empty($_POST['enquiry_details']) || empty($_POST['email']) ) {
   
// here, they have not filled in either their name, email or enquiry details.  Set an error.
   
header("Location: error.htm"); exit;
}
else
// Pick up the form data and assign it to variables
$title $_POST['title'];
$name $_POST['name'];

// check to see whether common injection terms are entered into certain fields
function contains_bad_str($str_to_test)   {
  
$bad_strings = array(
                
"content-type:"
                
,"mime-version:"
                
,"multipart/mixed"
        
,"Content-Transfer-Encoding:"
                
,"bcc:"
        
,"cc:"
        
,"to:"
  
);
  
// if terms are present, set error and exit
  
foreach($bad_strings as $bad_string) {
    if(
eregi($bad_stringstrtolower($str_to_test) )) {
      echo 
"Suspected injection attempt - mail not being sent.";
      exit;
    }
  }
}
// check for newlines
function contains_newlines($str_to_test) {
   if(
preg_match("/(%0A|%0D|\\n+|\\r+)/i"$str_to_test) != 0) {
     echo 
"Suspected injection attempt - mail not being sent.";
     exit;
   }
}

contains_bad_str($title);
contains_bad_str($name);
contains_bad_str($email);
contains_bad_str($phone);
contains_bad_str($address);
contains_bad_str($heard_from);
contains_bad_str($heard_other);
contains_bad_str($style);
contains_bad_str($enquiry_details);

contains_newlines($title);
contains_newlines($name);
contains_newlines($email);
contains_newlines($phone);
contains_newlines($heard_from);
contains_newlines($heard_other);

$email $_POST['email'];

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/"$email))
{
    
header("Location: error.htm");
    exit;
}

$phone $_POST['phone'];
$address $_POST['address'];
$heard_from $_POST['heard_from'];
$heard_other $_POST['heard_other'];
$style $_POST['style'];
$enquiry_details $_POST['enquiry_details'];

// Build the email (replace the address in the $to section with your own)
$to 'me@example.com';
$subject "Contact form enquiry";
$message "Title:$title,

Name:
$name,

Phone:
$phone,

Address: 
$address,

Heard from: 
$heard_from,

Heard other: 
$heard_other,

Style: 
$style,

Enquiry details: 
$enquiry_details,";
$headers "From: $email";

// Send the mail using PHPs mail() function
// succe=false or true; = return values from mail() function
$succe mail($to$subject$message$headers);

// Redirect
if($succe)
{
    
header("Location: confirmation.htm");
    exit(); 
// redirect to confirmation page and end php
}
else
{
    
// end php, with a message of failure
    
exit("Sorry. Mail was not sent. Go Back, try again");
}
?>
jcsj is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-12-2008, 06:50 PM Re: HTML form to Email using PHP - Help a newbie pleasie!
vectorialpx's Avatar
Extreme Talker

Posts: 241
Name: octavian
Location: Bucharest
Trades: 0
add a captcha but... you don't need such protection, for strings like "content-type" or stuff.. the message is a string and this will not affect you. See www.vectorialpx.net... and try to send me some other headers

I have a "3 mails per session" politic
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 05-13-2008, 06:27 PM Re: HTML form to Email using PHP - Help a newbie pleasie!
Junior Talker

Posts: 2
Name: John
Location: North Wales
Trades: 0
Thanks for the response. I do intend to add a captcha, just learning as I go - nice to know that I am going along the right lines.

I wasn't sure about 'bc, cc, content type' so just added it to every field - I guess I don't need this if I use a email regex on the php side? (do have a email regex on the javascript but it never dawned on me to use it on the PHP).

Had a look at your form, you captcha is cool (probably the best I've seen) that is probably a bit beyond me at the moment but I will get there in the end!

See what you mean about sending you some headers!

Is your form on a php page or html with javascript (sorry if thats a dumb question!).

M
jcsj is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to HTML form to Email using PHP - Help a newbie pleasie!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41264 seconds with 12 queries