Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Użycie reCaptcha od google
Forum PHP.pl > Forum > Przedszkole
Adalberd
Nie jestem programistą, uczę się sam krok po korku.W związku z atakiem botów na mój formularz kontaktowy, chce się zabezpieczyć za pomocą reCaptcha v2 od Google. Udało mi się osadzić ją w formularzu ale nie wiem w którym miejscu i jak ją obsłużyć w takim skrypcie (w komentarzu coś co udało mi się w poradniku przeczytać ale nie wiem jak to użyć w tym przypadku, gdzie sprawdzać $result):
  1. <?php
  2.  
  3. $ciag='!QAZ2wsx@#$-P';
  4. $odp1 = $_POST['miasto'];
  5. if (empty($_POST['submit'])) {
  6. <table border=\"0\" class=\"menu\"><form method=\"post\">
  7. <tr>
  8. <td>Message</td>
  9. <td><textarea name=\"tresc\" style=\"width: 350px; height: 140px; padding:10px 10px;border: 1px double #d4d4d4; color:#000000;box-shadow: 0px 1px 5px #acacac;\"></textarea></td>
  10. </tr>
  11. <tr>
  12. <td>Name</td>
  13. <td><input type=\"text\" name=\"imie\" style=\"width: 350px; padding:10px 10px;border: 1px double #d4d4d4; color:#000000;box-shadow: 0px 1px 5px #acacac;\"></td>
  14. </tr>
  15.  
  16. <td>Phone</td>
  17. <td><input type=\"text\" name=\"telefon\" style=\"width: 350px; padding:10px 10px;border: 1px double #d4d4d4; color:#000000;box-shadow: 0px 1px 5px #acacac;\"></td>
  18. </tr>
  19. <tr>
  20. <td>E-mail</td>
  21. <td><input type=\"menu\" name=\"email\" style=\"width: 350px; padding:10px 10px;border: 1px double #d4d4d4; color:#000000;box-shadow: 0px 1px 5px #acacac;\"></td>
  22. </tr>
  23. <tr>
  24. <td></td>
  25. <td><input type=\"text\" name=\"nazwisko\" style=\"display:none;\"></td>
  26. <td><input type=\"text\" name=\"miasto\" style=\"display:none;\" value=$ciag></td>
  27. </tr>
  28. <td> </td>
  29. <td><div class=\"g-recaptcha\" data-size=\"compact\" data-sitekey=\"xxxxxxxxxxxxxx\"></div></td>
  30. <td><input type=\"submit\" name=\"submit\" class=\"buton\" value=\"Send e-mail\">
  31. <input type=\"reset\" class=\"buton\" value=\"Clean form\"></td></form>
  32. </tr>
  33. </table>
  34.  
  35. "
  36.  
  37. ;
  38.  
  39. /*
  40.   $secret = 'xxxxx';
  41.   $response = $_POST['g-recaptcha-response'];
  42.   $remoteip = $_SERVER['REMOTE_ADDR'];
  43.  
  44.   $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
  45.   $result = json_decode($url, TRUE);
  46.   if ($result['success'] == 1) {
  47.   echo 'Nie jesteś botem';
  48.   }else{
  49.   echo
  50.  'Błędnie wypełnione pole reCAPTCHA';
  51.   }
  52. } */
  53.  
  54.  
  55. }elseif (!empty($_POST['tresc']) && !empty($_POST['imie']) && !empty($_POST['email']) && !empty($_POST['telefon']) && empty($_POST['nazwisko']) ) {
  56.  
  57.  
  58. $email=$_POST['email'];
  59.  
  60.  
  61.  
  62. if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email) OR $odp1 != $ciag)
  63. {
  64. echo "<span class=\"menu\">Attention Error !! Introduced web address is incorrect !<br> Please enter the correct web address.<br><br> <a href=\"?cat=contact\" class=\"link\">Return to the form</font></span>";
  65. /*echo $odp1; echo $ciag;*/ echo $responseKeys;
  66. } else
  67. {
  68.  
  69.  
  70. $message = "Message:\n$_POST[tresc]\n\nName: $_POST[imie] \nPhone: $_POST[telefon] \nE-mail: $_POST[email]";
  71.  
  72. $header = "From: $_POST[imie] <$_POST[email]>";
  73.  
  74. @mail("xxx@xxx.pl","Contact form website Asia Connecting Center","$message","$header")
  75. or die('The message could not be delivered');
  76.  
  77. echo "<div align=\"center\" class=\"menu\"><strong>The message has been successfully delivered !</strong><br><br></div>";
  78. echo "<a href=\"?id=contact\" class=\"link\">Return to the form</a></span>";
  79.  
  80. }
  81. }
  82. else echo "<span class=\"menu\">Attention Error !! Please fill in all blanks in a form ! <br><br><br> <a href=\"?cat=contact\" class=\"link\">Return to the form</font></span>";
  83.  
  84. ?>
shpaque
  1. if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['formName'] == 'loginForm'))
  2. {
  3. if (isset($_POST['g-recaptcha-response'])) {$captcha = $_POST['g-recaptcha-response'];}
  4. if (!$captcha)
  5. {
  6. echo "
  7. <script>
  8. alert('Proszę zaznaczyć pole reCaptcha');
  9. window.location.href = \"./jakis plik\";
  10. </script>";
  11. }
  12. else
  13. {
  14. $secretKey = "blebleble";
  15. $ip = $_SERVER['REMOTE_ADDR'];
  16. $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
  17. $responseKeys = json_decode($response, true);
  18.  
  19. if (intval($responseKeys['success']) !== 1)
  20. {
  21. echo "<script>alert('esteś spamerem! Lepiej już sobie idź!');</script>";
  22. }
  23. else
  24. {
  25. echo "ok";
  26. }
  27. }
  28. }


to dzialajacy przyklad
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.