Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem z reCaptcha od google
koko887
post 3.10.2020, 14:30:49
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 23.09.2009

Ostrzeżenie: (0%)
-----


Witam wszystkich.

Chciałem dodać na swoją stronę walidację recaptcha, żeby boty dały mi spokój. Niestety siedzę nad tym kilka dni i nie wiem czemu nie działa. Strona jest podpięta pod CLOUDFLARE i może to generuje jakieś problemy ale kombinowałem z różnymi kodami i nie działa. Z tego co widzę, to jest problem, z tym, że przez POST nie wysyła mi " $_POST['g-recaptcha-response']" i cała walidacja nie może zakończyć się pomyślnie. Nie wiem z czego to wynika.

Poniżej moje kody:

plik contact.php
  1. <?php
  2. include 'functions.php';
  3.  
  4. if (!empty($_POST)){
  5.  
  6. $data['success'] = true;
  7. //$_POST = multiDimensionalArrayMap('cleanEvilTags', $_POST);
  8. //$_POST = multiDimensionalArrayMap('cleanData', $_POST);
  9.  
  10.  
  11. $emailTo ='kontakt@test.pl';
  12. $emailSubject = 'Wiadomość z formularza';
  13. $name = $_POST['name'];
  14. $email = $_POST['email'];
  15. $comment = $_POST['comments'];
  16.  
  17.  
  18.  
  19. if($name == '') {
  20. echo '<div class="error_message">Proszę podać swoje imię.</div>'; exit();}
  21.  
  22. if($email == '') {
  23. echo '<div class="error_message">Proszę podać adres email.</div>'; exit();}
  24.  
  25. if (!preg_match('/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i', $email)) {
  26. echo '<div class="error_message">Podałeś błędny adres email.</div>'; exit();}
  27.  
  28. if($comment == '') {
  29. echo '<div class="error_message">Proszę podać treść wiadomości.</div>'; exit();}
  30.  
  31.  
  32. if($data['success'] == true) {
  33.  
  34. $send_name = "Imię: $name" . PHP_EOL . PHP_EOL;
  35. $send_email = "Email: $email" . PHP_EOL . PHP_EOL;
  36. $send_comment = "Wiadomość:\r\n$comment" . PHP_EOL . PHP_EOL;
  37. $message = wordwrap( $send_name . $send_email . $send_comment, 70 );
  38.  
  39. $headers = "From: TEST.PL <$email>" . PHP_EOL;
  40. $headers .= "Reply-To: $email" . PHP_EOL;
  41. $headers .= "MIME-Version: 1.0" . PHP_EOL;
  42. $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
  43. $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
  44.  
  45. mail($emailTo, $emailSubject, $message, $headers);
  46. echo "<fieldset>";
  47. echo "<div id='success_page'>";
  48. echo "<h3>WIADOMOŚĆ WYSŁANO POPRAWNIE.</h3>";
  49. echo "<p>Dziękuję <strong>$name</strong> za kontakt, Twoja wiadomość została wysłana poprawnie :)</p>";
  50. echo "</div>";
  51. echo "</fieldset>";}
  52.  
  53. else { echo '<div class="error_message">CAPTCHA</div>'; }
  54. }
  55. ?>



kod formularza:
  1. <h2>FORMULARZ KONTAKTOWY</h2>
  2. <div class="separator-image-contact"></div>
  3. <div id="contact-form">
  4. <div id="message"></div>
  5. <form method="post" action="php/contact.php" name="contactform" id="contactform">
  6. <input type="text" id="name" name="name" placeholder="Twoje imię..." />
  7. <input type="text" name="email" id="email" placeholder="Twój email..." />
  8. <textarea name="comments" id="comments" placeholder="Treść Twojej wiadomości..."></textarea>
  9. <div class="g-recaptcha" data-sitekey="6LcxxxxxxxxxxDkXk5g"></div>
  10. <input type="submit" class="send_message transition" id="submit" value="Wyślij wiadomość" />
  11. </form>
  12. </div>



kod od reCaptcha:
  1. $secret = '6Lcxxxxxxxxxx82';
  2. $response = $_POST['g-recaptcha-response'];
  3. $remoteip = $_SERVER['REMOTE_ADDR'];
  4.  
  5. $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
  6. $result = json_decode($url, TRUE);
  7. if ($result['success'] == true) {
  8.  
  9. COŚ MA SIĘ ZADZIAĆ :(
  10.  
  11. }



plik functions.php:
  1. <?php
  2.  
  3. // Strips nasty tags from code..
  4. function cleanEvilTags($data) {
  5. $data = preg_replace("/javascript/i", "javascript",$data);
  6. $data = preg_replace("/alert/i", "alert",$data);
  7. $data = preg_replace("/about:/i", "about:",$data);
  8. $data = preg_replace("/onmouseover/i", "onmouseover",$data);
  9. $data = preg_replace("/onclick/i", "onclick",$data);
  10. $data = preg_replace("/onload/i", "onload",$data);
  11. $data = preg_replace("/onsubmit/i", "onsubmit",$data);
  12. $data = preg_replace("/<body/i", "&lt;body",$data);
  13. $data = preg_replace("/<html/i", "&lt;html",$data);
  14. $data = preg_replace("/document\./i", "document.",$data);
  15. $data = preg_replace("/<script/i", "&lt;script",$data);
  16. return strip_tags(trim($data));
  17. }
  18.  
  19. // Cleans output data..
  20. function cleanData($data) {
  21. $data = str_replace(' & ', ' &amp; ', $data);
  22. return (get_magic_quotes_gpc() ? stripslashes($data) : $data);
  23. }
  24.  
  25. function multiDimensionalArrayMap($func,$arr) {
  26. $newArr = array();
  27. if (!empty($arr)) {
  28. foreach($arr AS $key => $value) {
  29. $newArr[$key] = (is_array($value) ? multiDimensionalArrayMap($func,$value) : $func($value));
  30. }
  31. }
  32. return $newArr;
  33. }



Proszę o pomoc. Kombinowałem z różnymi kodami reCaptcha, ale zawsze było tak, że jest błąd jakby się nie zaznaczyło ptaszka (mimo, że był zaznaczony) i ustaliłem, że problem jest z "$_POST['g-recaptcha-response']". W błędach PHP na serwerze wyświetla się wtedy linijka zawierająca "Undefined index g-recaptcha-response", niestety nie wiem co z tym zrobić.

Bardzo proszę o pomoc smile.gif


Ten post edytował koko887 3.10.2020, 14:36:39
Go to the top of the page
+Quote Post
viking
post 3.10.2020, 15:10:43
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Osadziłeś skrypt js na stronie?
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Do czyszczenia zastosuj sprawdzone biblioteki np htmlpurifier a nie takie coś.


--------------------
Go to the top of the page
+Quote Post
koko887
post 3.10.2020, 16:19:10
Post #3





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 23.09.2009

Ostrzeżenie: (0%)
-----


Tak, nie wspomniałem o tym, ale skrypt jest dodany (testowany zarówno w head jak i przed znacznikiem body).
Go to the top of the page
+Quote Post
viking
post 3.10.2020, 17:33:53
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Daj var_dump($_POST); w pierwszej linii contact.php


--------------------
Go to the top of the page
+Quote Post
koko887
post 3.10.2020, 19:44:50
Post #5





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 23.09.2009

Ostrzeżenie: (0%)
-----


Takie coś wypluwa:

  1. array(3) { ["name"]=> string(5) "Imię" ["email"]=> string(14) "email@email.pl" ["comments"]=> string(19) "Treść wiadomości" }
Go to the top of the page
+Quote Post
viking
post 3.10.2020, 19:56:51
Post #6





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


I jak widzisz żadnych danych ze skryptu google kótry powinien tworzyć element nie ma. Sprawdź konsolę przeglądarki, zobacz błędy i ten element recaptcha.


--------------------
Go to the top of the page
+Quote Post
koko887
post 3.10.2020, 20:32:18
Post #7





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 23.09.2009

Ostrzeżenie: (0%)
-----


Takie rzeczy się pokazują:

Go to the top of the page
+Quote Post
Mega_88
post 3.10.2020, 22:21:11
Post #8





Grupa: Zarejestrowani
Postów: 360
Pomógł: 34
Dołączył: 20.08.2011

Ostrzeżenie: (0%)
-----


Sprawdź konsolę (zakładka druga z lewej z Twojego screena tongue.gif ) w momencie kliknięcia w reCaptchę czy nie wypluwa żądnego błędu. Ostatnio też miałem problem z walidacją przez reCaptchę V2, a okazało się, że na serwerze klient nie miał włączonego allow_url_fopen i wywalało w konsoli "ReCAPTCHA couldn't find user-provided function: onReCaptchaSuccess", więc też możesz sprawdzić w tym kierunku.

Ten post edytował Mega_88 3.10.2020, 22:21:56
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.04.2024 - 21:20