[PHP] Inserire google recaptcha in uno form contatti

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
43
Massa, Italy
Salve, vorrei inserire il google Invisible reCAPTCHA .. ho trovato questo esempio:
https://github.com/geordyjames/google-Invisible-reCAPTCHA

Lo inserito nel mio codice .. ma se provo mi da:
An Error Occured and Error code is :missing-input

questo è il codice:
PHP:
<?php
include("inc.php");
include("include/inc_browser.php");
include("include/inc_ipinfo.io.php");
$title ="Contatti";
$layout = new layout;
$layout->setTitle($title);
$layout->startBody();
?>
<h1 class="title_page">Contatti</h1>
<div id="contact">
    <fieldset>
        <?php
        if(isset($_POST["invia"]))
        {
            $nome = $_POST["nome"];
            $cognome = $_POST["cognome"];
            $oggetto = $_POST["oggetto"];
            $messaggio =$_POST["messaggio"];
            $email = $_POST["email"];

            if(empty($nome))
            {
                $error = "Non hai inserito il nome";
            }
            elseif(empty($cognome))
            {
                $error = "Non hai inserito il cognome";
            }
            elseif(empty($oggetto))
            {
                $error = "Non hai inserito l'oggetto";
            }
            elseif(empty($email))
            {
                $error = "Inserire il l'email";
            }
            elseif(!valid_email($email))
            {
                $error = "Inserire la tua email valida";
            }

            elseif(empty($messaggio))
            {
                $error = "Non hai inserito il messaggio";
            }
            else

            {
                require("classs/class.google.php");
                $recaptcha = $_POST['g-recaptcha-response'];
                $object = new Recaptcha();
                $response = $object->verifyResponse($recaptcha);
                if(isset($response['success']) and $response['success'] != true) {
                    echo "An Error Occured and Error code is :".$response['error-codes'];
                }else {
                    require("classs/class.phpmailer.php");
                    require("classs/class.smtp.php");

                    $mail = new PHPMailer();

                    $mail->IsSMTP(); // abilitiamo l'SMTP
                    $mail->SMTPDebug = 0;  // debug: 1 = solo messaggi, 2 = errori e messaggi
                    $mail->SMTPAuth = true;  // abilitiamo l'autenticazione
                    $mail->SMTPSecure = 'ssl'; // abilitiamo il protocollo ssl richiesto per Gmail
                    $mail->Host = $admin_email_host; // ecco il server smtp di google
                    $mail->Port = $admin_email_port; // la porta che dobbiamo utilizzare



                    // Inseriamo i dati della casella che utilizzeremo per spedire

                    $mail->Username = $admin_email_username;

                    $mail->Password = $admin_email_password;


                    $mail->From = $_POST["email"];

                    $mail->FromName = $_POST["nome"]." ".$_POST["cognome"];

                    // Qui inseriamo l'indirizzo a cui recapitare le email spedite tramite il form

                    $mail->AddReplyTo($_POST["email"], $_POST["nome"]."  ".$_POST["cognome"]);


                    $mail->AddAddress($admin_email);

                    $mail->IsHTML(true);

                    $messaggio = "Nome:&nbsp;&nbsp;". $_POST["nome"]."<br />";
                    $messaggio .= "Cognome:&nbsp;&nbsp;". $_POST["cognome"]."<br />";
                    $messaggio .= "Indirizzo Ip:&nbsp;&nbsp;". $_SERVER["REMOTE_ADDR"]."<br />";
                    $browser = getBrowser($_SERVER['HTTP_USER_AGENT']);
                    $ip = $_SERVER["REMOTE_ADDR"];
                    $loc = _ipinfo($ip);
                    $messaggio .= "Location: ".$loc. "<br />";
                    $messaggio .= "Browser:&nbsp;&nbsp;". $browser."&nbsp;&nbsp;<br />";
                    $messaggio .= "Messaggio: <br />". nl2br(replace_text($_POST["messaggio"]))."<br />";
                    $messaggio .= "=====================================================================";

                    $mail->Body= $messaggio;

                    $oggetto = "Richiesta da ".$www_email." : ".$_POST["oggetto"]."";

                    $mail->Subject=$oggetto;


                    if($mail->Send()){

                        $msg = "ok, attendere prego..al ritorno della home page. ";

                        redirect("index.php",4);

                    }else{

                        $error = "Errore invio mail";

                    }
                }
            }
        }
        $config = require('config.php');
        $nome = (isset($_POST["nome"])) ? trim(($_POST["nome"])): '';
        $cognome = (isset($_POST["cognome"])) ? trim(($_POST["cognome"])): '';
        $oggetto = (isset($_POST["oggetto"])) ? trim(($_POST["oggetto"])): '';
        $messaggio = (isset($_POST["messaggio"])) ? trim(($_POST["messaggio"])): '';
        $email = (isset($_POST["email"])) ? trim(($_POST["email"])): '';
        ?>
        <script src='https://www.google.com/recaptcha/api.js'></script>
         <script>
       function onSubmit() {
         document.getElementById("contact-form").submit();
       }
    </script>
        <?php
        if(isset($error)){ echo '<p class="info_error">'.$error.'</p>';}
        if(isset($msg)){ echo $msg;} else {
            ?>           
            <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="contact-form">
                <p>Nome</p>
                <p><input type="text" name="nome"  class="form-control" value="<?php if(isset($_POST['nome'])){echo htmlspecialchars($_POST['nome']);}else { echo htmlspecialchars($nome); }?>" /></p>
                <p>Cognome</p>
                <p><input type="text" name="cognome" class="form-control"  value="<?php if(isset($_POST['cognome'])){echo htmlspecialchars($_POST['cognome']);}else { echo htmlspecialchars($cognome); }?>"  /></p>
                <p>Oggetto</p>
                <p><select name="oggetto" class="form-control">
                    <option value="Informazioni"   <?php if ($oggetto == "Informazioni") echo ' selected' ?>>Informazioni</option>
                    <option value="Richiesta di contatto"    <?php if ($oggetto == "Richiesta di contatto") echo ' selected' ?>>Richiesta di contatto</option>
                    <option value="Richiesta Curriculum Vitae"    <?php if ($oggetto == "Richiesta Curriculum Vitae") echo ' selected' ?>>Richiesta Curriculum Vitae</option>
                    <option value="Altro"    <?php if ($oggetto == "Altro") echo ' selected' ?>>Altro</option>
                </select>
            </p>
            <p>Email</p>
            <p><input type="text" name="email" class="form-control" size="40" value="<?php if(isset($_POST['email'])){echo htmlspecialchars($_POST['email']);}else { echo htmlspecialchars($email); }?>" /></p>
            <p>Messaggio</p>
            <p><textarea rows="10" cols="40"  class="form-control"name="messaggio"><?php if(isset($_POST['messaggio'])){echo $_POST['messaggio'];}else { echo $messaggio; }?></textarea></p>
            <p><button class="g-recaptcha"     data-sitekey="<?php echo $config['client-key']; ?>" data-callback="onSubmit"></button></p>
            <p><input type="submit" name="invia" value="invia"></p>
        </form>
    <?php } ?>
</fieldset>
</div>
<?php
$layout->endBody();
echo $layout->render('theme/'.$theme.'/template.php');
?>

Le chiavi le ho creato adesso .. per caso devo aspettare le chiavi che si attivano?
Grazie mille e buon sabato.
 
risolto.. era l'engine template che non andava bene e ho dovuto cambiare metodo..
vi posto il codice anche se non avete altre parti del codice:

PHP:
<?php
/* http://www.onlinecode.org/google-invisible-recaptcha-using-php/ */
include("inc.php");
include("include/inc_browser.php");
include("include/inc_ipinfo.io.php");
$title ="Contatti";
include("theme/".$theme."/header.php");
?>
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <?php
      echo "<div id=\"f\">\n";
      echo "<div id=\"pages\">\n";
      ?>

      <h1 class="title_page">Contatti</h1>
      <div id="contact">
        <fieldset>
          <?php
          if($_SERVER["REQUEST_METHOD"] === "POST")
          {
            $nome = $_POST["nome"];
            $cognome = $_POST["cognome"];
            $oggetto = $_POST["oggetto"];
            $messaggio =$_POST["messaggio"];
            $email = $_POST["email"];

            if(empty($nome))
            {
              $error = "Non hai inserito il nome";
            }
            elseif(empty($cognome))
            {
              $error = "Non hai inserito il cognome";
            }
            elseif(empty($oggetto))
            {
              $error = "Non hai inserito l'oggetto";
            }
            elseif(empty($email))
            {
              $error = "Inserire il l'email";
            }
            elseif(!valid_email($email))
            {
              $error = "Inserire la tua email valida";
            }

            elseif(empty($messaggio))
            {
              $error = "Non hai inserito il messaggio";
            }
            else

            {
              //verify captcha code process
              // add your secret key
              $google_recaptcha_secret = $google_secretkey;

              // pass secret key in google recaptcha api url
              $api_response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$google_recaptcha_secret."&response=".$_POST['g-recaptcha-response']);

              // json decode for data
              $api_response = json_decode($api_response, true);

              if($api_response["success"] === true)
              {

                require("classs/class.phpmailer.php");
                require("classs/class.smtp.php");

                $mail = new PHPMailer();

                $mail->IsSMTP(); // abilitiamo l'SMTP
                $mail->SMTPDebug = 0;  // debug: 1 = solo messaggi, 2 = errori e messaggi
                $mail->SMTPAuth = true;  // abilitiamo l'autenticazione
                $mail->SMTPSecure = 'ssl'; // abilitiamo il protocollo ssl richiesto per Gmail
                $mail->Host = $admin_email_host; // ecco il server smtp di google
                $mail->Port = $admin_email_port; // la porta che dobbiamo utilizzare



                // Inseriamo i dati della casella che utilizzeremo per spedire

                $mail->Username = $admin_email_username;

                $mail->Password = $admin_email_password;


                $mail->From = $_POST["email"];

                $mail->FromName = $_POST["nome"]." ".$_POST["cognome"];

                // Qui inseriamo l'indirizzo a cui recapitare le email spedite tramite il form

                $mail->AddReplyTo($_POST["email"], $_POST["nome"]."  ".$_POST["cognome"]);


                $mail->AddAddress($admin_email);

                $mail->IsHTML(true);

                $messaggio = "Nome:&nbsp;&nbsp;". $_POST["nome"]."<br />";
                $messaggio .= "Cognome:&nbsp;&nbsp;". $_POST["cognome"]."<br />";
                $messaggio .= "Indirizzo Ip:&nbsp;&nbsp;". $_SERVER["REMOTE_ADDR"]."<br />";
                $browser = getBrowser($_SERVER['HTTP_USER_AGENT']);
                $ip = $_SERVER["REMOTE_ADDR"];
                $loc = _ipinfo($ip);
                $messaggio .= "Location: ".$loc. "<br />";
                $messaggio .= "Browser:&nbsp;&nbsp;". $browser."&nbsp;&nbsp;<br />";
                $messaggio .= "Messaggio: <br />". nl2br(replace_text($_POST["messaggio"]))."<br />";
                $messaggio .= "=====================================================================";

                $mail->Body= $messaggio;

                $oggetto = "Richiesta da ".$www_email." : ".$_POST["oggetto"]."";

                $mail->Subject=$oggetto;


                if($mail->Send()){

                  $msg = "ok, attendere prego..al ritorno della home page. ";

                  redirect("index.php",4);

                }else{

                  $error = "Errore invio mail";

                }

              }
              else
              {
                // not success result
                $error = "You are a robot";
              }
            }
          }
          $nome = (isset($_POST["nome"])) ? trim(($_POST["nome"])): '';
          $cognome = (isset($_POST["cognome"])) ? trim(($_POST["cognome"])): '';
          $oggetto = (isset($_POST["oggetto"])) ? trim(($_POST["oggetto"])): '';
          $messaggio = (isset($_POST["messaggio"])) ? trim(($_POST["messaggio"])): '';
          $email = (isset($_POST["email"])) ? trim(($_POST["email"])): '';
          ?>
          <script src='https://www.google.com/recaptcha/api.js'></script>
          <?php
          if(isset($error)){ echo '<p class="info_error">'.$error.'</p>';}
          if(isset($msg)){ echo $msg;} else {
            ?>
            <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="reCaptchaForm">
              <p>Nome</p>
              <p><input type="text" name="nome"  class="form-control" value="<?php if(isset($_POST['nome'])){echo htmlspecialchars($_POST['nome']);}else { echo htmlspecialchars($nome); }?>" /></p>
              <p>Cognome</p>
              <p><input type="text" name="cognome" class="form-control"  value="<?php if(isset($_POST['cognome'])){echo htmlspecialchars($_POST['cognome']);}else { echo htmlspecialchars($cognome); }?>"  /></p>
              <p>Oggetto</p>
              <p><select name="oggetto" class="form-control">
                <option value="Informazioni"   <?php if ($oggetto == "Informazioni") echo ' selected' ?>>Informazioni</option>
                <option value="Richiesta di contatto"    <?php if ($oggetto == "Richiesta di contatto") echo ' selected' ?>>Richiesta di contatto</option>
                <option value="Richiesta Curriculum Vitae"    <?php if ($oggetto == "Richiesta Curriculum Vitae") echo ' selected' ?>>Richiesta Curriculum Vitae</option>
                <option value="Altro"    <?php if ($oggetto == "Altro") echo ' selected' ?>>Altro</option>
              </select>
            </p>
            <p>Email</p>
            <p><input type="text" name="email" class="form-control" size="40" value="<?php if(isset($_POST['email'])){echo htmlspecialchars($_POST['email']);}else { echo htmlspecialchars($email); }?>" /></p>
            <p>Messaggio</p>
            <p><textarea rows="10" cols="40"  class="form-control"name="messaggio"><?php if(isset($_POST['messaggio'])){echo $_POST['messaggio'];}else { echo $messaggio; }?></textarea></p>
            <div class="g-recaptcha" data-sitekey="<?php echo $google_sitekey; ?>"></div>
            <input type="submit" value="Invia contatto" class="btn btn-primary submit" name="submit" />
          </form>
          <?php } ?>
        </fieldset>
      </div>
      <?php
      echo "</div>\n";
      echo "</div>\n";
      ?>
    </div>
  </div>
</div>
<?php
include("theme/".$theme."/footer.php");
?>
 

Discussioni simili