Autor Zpráva
aa33kk
Profil
Ahoj, kontroluji přes JS, zda je vyplněná reCAPTCHA či nikoliv a na podkladě toho mi dále zpracovává odeslání formuláře. I přesto mi chodí SPAM na e-mail. Je možné, že mám někde krpu? Už do toho hledím nějaký den a nejsem schopný na nic kloudného přijít.

JS script

$(function() {
  
  $("#contact-form input,#contact-form textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
      // additional error messages or events
    },
    submitSuccess: function($form, event) {
      event.preventDefault(); // prevent default submit behaviour
      // get values from FORM
      var name = $("input#Name").val();
      var email = $("input#Email").val();
      var message = $("textarea#Message").val();
      var firstName = name; // For Success/Failure Message
      // Check for white space in name for Success/Fail message
      if (firstName.indexOf(' ') >= 0) {
        firstName = name.split(' ').slice(0, -1).join(' ');
      }
      $this = $("#sendMessageButton");
      if (grecaptcha.getResponse() == ''){
                      // if error I post a message in a div
                      alert('Potvrďte prosím, že nejste robot.');

                  } else {
      $this.prop("disabled", true); // Disable submit button until AJAX call is complete to prevent duplicate messages
      
      $.ajax({
        url: "contactengine.php",
        type: "POST",
        data: {
          name: name,
          email: email,
          message: message
        },
        cache: false,
        success: function() {
          // Success message
          $('#success').html("<div class='alert alert-success'>");
          $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
            .append("</button>");
          $('#success > .alert-success')
            .append("<strong>Vaše zpráva byla odeslána. </strong>");
          $('#success > .alert-success')
            .append('</div>');
          //clear all fields
          $('#contactForm').trigger("reset");
        },
        error: function() {
          // Fail message
          $('#success').html("<div class='alert alert-danger'>");
          $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
            .append("</button>");
          $('#success > .alert-danger').append($("<strong>").text("Promiň " + firstName + ", ale zdá se, že poštovní server neodpovídá. Prosím, zkus to později znovu!"));
          $('#success > .alert-danger').append('</div>');
          //clear all fields
          $('#contactForm').trigger("reset");
        },
        complete: function() {
          setTimeout(function() {
            $this.prop("disabled", false);
          }, 1000);
        }
      });}
    },
    filter: function() {
      return $(this).is(":visible");
    },
  });

  $("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
  });
});

$('#name').focus(function() {
  $('#success').html('');
});
                             

PHP zpracování

<?php

$EmailFrom     = "email"
$EmailTo     = "email"
$Subject     = "Dotaz z webového formuláře";
$Name         = Trim( stripslashes( $_POST[ 'Name' ] ) );
$Email         = Trim( stripslashes( $_POST[ 'Email' ] ) );
$Message     = Trim( stripslashes( $_POST[ 'Message' ] ) );

// validation
$validationOK = true;
if ( !$validationOK ) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
}

// prepare email body text
$Body = "";
$Body .= "Jméno: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Dotaz: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail( $EmailTo, $Subject, $Body, "From: <$EmailFrom>" );

// redirect to success page 
if ( $success ) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
} else {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}

Díky za jakýkoliv podnět.
Radek9
Profil
aa33kk:
Co se asi tak stane, když se vykašlu na ten tvůj krásný JS script a pošlu formulář rovnou (bez jakéhokoli ošetření) na contactengine.php? Tu captchu musíš kontrolovat i v PHP, jinak se dá přesně takhle obejít.
aa33kk
Profil
Radek9:
Díky za info. Mohlo by být takto?

<?php
 
$EmailFrom     = "email";
$EmailTo     = "email";
$Subject     = "Dotaz z webového formuláře";
$Name         = Trim( stripslashes( $_POST[ 'Name' ] ) );
$Email         = Trim( stripslashes( $_POST[ 'Email' ] ) );
$Message     = Trim( stripslashes( $_POST[ 'Message' ] ) );
$recaptcha = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=MujSiteKey&response=get' . $_POST['g-recaptcha-response'])); 
 
 
// validation
$validationOK = true;
if ( !$validationOK ) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
}
 
// prepare email body text
$Body = "";
$Body .= "Jméno: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Dotaz: ";
$Body .= $Message;
$Body .= "\n";
 
// send email
if ($recaptcha->{'success'} == 'true') {
        $success = mail( $EmailTo, $Subject, $Body, "From: <$EmailFrom>" );;
} else {
        echo('Uživatel není člověk.');
}
 
// redirect to success page 
if ( $success ) {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
} else {
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
Keeehi
Profil
aa33kk:
No a funguje ti to? Asi ne, že.

Zaprvé vždy to skončí v bloku ř. 12 - 17.
Dále v url na ř. 9 ti zůstal bordel. response=get
V dokumentaci se píše, že bys měl posílat POST požadavek ale různě po fórech jsem dohledal, že to bere i GET. Takže to je taková chyba nechyba. Sice ti to fungovat asi bude ale může to přestat kdykoli v budoucnu.
Jinak by to téměř šlo. Až na řádek 39 kde proměnná success nebude definovaná, pokud captcha neuspěje.

if ($recaptcha->{'success'} == 'true') {
        if(mail( $EmailTo, $Subject, $Body, "From: <$EmailFrom>" )) {
            print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
        } else {
            print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
        }
} else {
        echo('Uživatel není člověk.');
}

Kód na pohled to hezký není, ale chápu že jsi rád že ti to funguje alespoň trochu.
Daniel47
Profil
aa33kk:
Kód pro reCAPTCHU:

if(isset($_POST['g-recaptcha-response'])) {
$recaptcha = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=VášTajnýKlíč&response=' . $_POST['g-recaptcha-response'])); 
if($recaptcha->{'success'} == 'true') { 
echo ('<b>Nejste robot.</b>');
//Zde bude kód aby se provedl, při zaškrtnuté reCAPTCHE
   } else {
echo('<b>Uživatel není člověk.</b>');
       }
  }
  

Vaše odpověď

Mohlo by se hodit

Neumíte-li správně určit příčinu chyby, vkládejte odkazy na živé ukázky.
Užíváte-li nějakou cizí knihovnu, ukažte odpovídajícím, kde jste ji vzali.

Užitečné odkazy:

Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0