Form code with honey pot not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

weavers
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2008 5:12 pm

Form code with honey pot not working?

Post by weavers »

Hi

I have the following form code, I have a honey pot form field which is hidden with css called 'Email' The problem is the email isn't being sent, can anyone point me in the right direction.

I'm feeling out of my depth!

Code: Select all

<?  
      $mail_content = 
      
      "Dear Joe Bloggs\n\n".
      
      "Please could you provide me with a quote, my details are as follows:\n\n".
 
        "Title: ".$_POST['Contact_Title']."\n".
        
        "Contact Name: ".$_POST['Contact_Name']."\n".
        
        "Company Name: ".$_POST['Company_Name']."\n".
 
        "Postcode: ".$_POST['Postcode']."\n\n".
        
        "E-mail: ".$_POST['Email2']."\n".
        
        "Telephone: ".$_POST['Contact_Telephone']."\n".
        
        "How did you find us: ".$_POST['find_us']."\n\n".
        
        "Im Interested: ".$_POST['Interested']."\n\n".
        
        "Message body: ".$_POST['Message_Body']."\n";
 
      echo nl2br($mail_content);    
 
    // Check the spam trap to see if it has data in it. If it does, don't bother mailing the form.
    if (!isset($_POST['Email'])) {
        mail("my@email.com", $Interested, $mail_content);
    }  
 
    ?>
Thanks for any help.
pinoypride
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 7:46 pm

Re: Form code with honey pot not working?

Post by pinoypride »

I don't know if you've just forgot to include this ();

Basically, I added this:

$mail_content = ( "Text1" . "Text2");

=======================

Here's the new code:

Code: Select all

<?php
 
$mail_content = (
 
"Dear Joe Bloggs\n\n".
"Please could you provide me with a quote, my details are as follows:\n\n".
 
"Title: ".$_POST['Contact_Title']."\n".
 
"Contact Name: ".$_POST['Contact_Name']."\n".
 
"Company Name: ".$_POST['Company_Name']."\n".
 
"Postcode: ".$_POST['Postcode']."\n\n".
 
"E-mail: ".$_POST['Email2']."\n".
 
"Telephone: ".$_POST['Contact_Telephone']."\n".
 
"How did you find us: ".$_POST['find_us']."\n\n".
 
"Im Interested: ".$_POST['Interested']."\n\n".
 
"Message body: ".$_POST['Message_Body']."\n"
);
 
echo nl2br($mail_content);  
 
// Check the spam trap to see if it has data in it. If it does, don't bother mailing the form.
   if(!isset($_POST['Email'])) 
     {
       mail("my@email.com", $Interested, $mail_content);
     }  
?>
weavers
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2008 5:12 pm

Re: Form code with honey pot not working?

Post by weavers »

Hi pinoypride

Thanks for the reply, I tried your suggestion but it broke the code and didn't display the confirmation page let alone send the email.

The form works fine if I remove the honey pot section:

if(!isset($_POST['Email']))
{
mail("my@email.com", $Interested, $mail_content); - (Not removing this line of course)
}


So i'm a bit stuck!

Many Thanks
pinoypride
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 7:46 pm

Re: Form code with honey pot not working?

Post by pinoypride »

Sorry, I misunderstood your post.

Code: Select all

// Check the spam trap to see if it has data in it. If it does, don't bother mailing the form.
- Were you trying not to send email if $_POST['email'] is not empty?

Try this:

if(empty($_POST['email']))

or if you are validating that variable that shouldn't be empty:

if(!empty($_POST['email']))
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Form code with honey pot not working?

Post by mmj »

I have the following form code, I have a honey pot form field which is hidden with css called 'Email' The problem is the email isn't being sent, can anyone point me in the right direction.
http://php.net/isset

Because it will always be sent, even if the user (or bot) doesn't fill it due to how isset works (see url above).

replace

Code: Select all

    if (!isset($_POST['Email'])) {
with

Code: Select all

    if (!$_POST['Email']) {
.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form code with honey pot not working?

Post by John Cartwright »

mmj wrote:

Code: Select all

if (!$_POST['Email']) {
This is NOT recommended because it will throw a notice if the variable does not exist. Instead as already suggested, use empty() instead of isset()
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Form code with honey pot not working?

Post by mmj »

Jcart wrote:
mmj wrote:

Code: Select all

if (!$_POST['Email']) {
This is NOT recommended because it will throw a notice if the variable does not exist. Instead as already suggested, use empty() instead of isset()
So just use @ if you are worried about E_NOTICE, will give you the same exact thing as empty.

I'm pretty sure it would be better performance wise too.
Last edited by mmj on Thu Nov 27, 2008 2:44 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form code with honey pot not working?

Post by John Cartwright »

mmj wrote:
Jcart wrote:
mmj wrote:

Code: Select all

if (!$_POST['Email']) {
This is NOT recommended because it will throw a notice if the variable does not exist. Instead as already suggested, use empty() instead of isset()
So just use @ if you are worried about E_NOTICE, will give you the same exact thing as empty.
Eeek! The usage of @ should be avoided unless completely necesary. There is a reason it is throwing a notice, because you are doing something wrong!
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Form code with honey pot not working?

Post by mmj »

Jcart wrote:Eeek! The usage of @ should be avoided unless completely unnecesary. There is a reason it is throwing a notice, because you are doing something wrong!
Haha. :mrgreen:

My views on this matter are different however, so we will respectfully disagree. :wink:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form code with honey pot not working?

Post by John Cartwright »

P.S. the @ is an "Error control operator" -- not the supress errors because I'm lazy operator :mrgreen:

It is not a matter of opinion, it is a matter of what is right and wrong. You are supressing an error because you are using a variable that does not exist. In fact, it is proper programming to ensure a variable exists before using it.

And if you disagree with that.. well... lets just leave it there :P

P.S., check your PHP error logs. They are probably massive if this is how you feel.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Form code with honey pot not working?

Post by mmj »

I agree but empty is just as much as surpressor as @ and has no "semantics".

If you were doing something like (isset($_POST['email']) && $_POST['email']) then that would make much more sense and also no errors would be shown.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form code with honey pot not working?

Post by John Cartwright »

mmj wrote:I agree but empty is just as much as surpressor as @ and has no "semantics".

If you were doing something like (isset($_POST['email']) && $_POST['email']) then that would make much more sense and also no errors would be shown.
Actually empty() provides the added benefit of checking the variable has a (positive) value, so that is incorrect.

Code: Select all

isset($_POST['email']) && $_POST['email']
is the exact same thing as writting

Code: Select all

!empty($_POST['email'])
So if I understand you correct, you are saying ignore the usage of isset() and empty() in favor of supressing errors?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Form code with honey pot not working?

Post by Eran »

empty() and isset() are not suppressors - they give you a chance to perform error handling. In general, it is bad practice to suppress errors.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Form code with honey pot not working?

Post by mmj »

Jcart wrote:Actually empty() provides the added benefit of checking the variable has a (positive) value, so that is incorrect.
:?:

...
pytrin wrote:empty() and isset() are not suppressors - they give you a chance to perform error handling. In general, it is bad practice to suppress errors.
I didn't say isset is a suppressor.

empty however, is a suppressor.
http://php.net wrote: empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
so

Code: Select all

(bool)$_POST['email']
equals

Code: Select all

!empty($_POST['email'])
just E_NOTICE errors are suppressed.

As said by the php manual!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Form code with honey pot not working?

Post by John Cartwright »

my goodness.

empty() does not supress the error, it will gracefully checks for the existance of the variable before using it. I'll have to look up the source code if this continues on, because I'll be damned if the PHP core generates internal errors in the source.

However, calling empty() an error supressor simply isn't right. It's purpose is not to supress errors.
Post Reply