Google recaptcha
Hallo, ik ben vandaag voor het eerst Google recaptcha v2 aan het gebruiken. De sitekey/secretkey etc. zijn aangemaakt. Ook doet de captcha het bij het contact formulier. Alleen nu ik de validatie code van mijn contact formulier heb moeten omgooien vanwege de capctcha ontvang ik nu geen mails meer:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/* Template Name: Contact */
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['image'] = get_field('image', $post->ID);
$context['address'] = get_field('address', $post->ID);
$context['address'] = str_replace(' ', '+', $context['address']);
$context['contact_address'] = get_field('contact_address', $post->ID);
$context['phone_number'] = get_field('phone_number', $post->ID);
$context['email'] = get_field('email', $post->ID);
$context['errors'] = [];
if(isset($_POST['send_contact_mail']) && !empty($_POST['send_contact_mail'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'secretcaptchakey';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
//contact form submission code
if(empty($_POST['firstName'])){
$context['errors']['firstName'] = 'Vul uw voornaam in';
} else {
$context['form']['firstName'] = $_POST['firstName'];
}
if(empty($_POST['lastName'])){
$context['errors']['lastName'] = 'Vul uw achternaam in';
} else {
$context['form']['lastName'] = $_POST['lastName'];
}
if(empty($_POST['callName'])){
$context['errors']['callName'] = 'Vul uw roepnaam in';
} else {
$context['form']['callName'] = $_POST['callName'];
}
if(empty($_POST['email'])){
$context['errors']['email'] = 'Vul uw email in';
} else {
$context['form']['email'] = $_POST['email'];
}
if(empty($_POST['comment'])){
$context['errors']['comment'] = 'Vul uw opmerking in';
} else {
$context['form']['comment'] = $_POST['comment'];
}
if(count($context['errors']) == 0){
sendTemplateMail('mail/contact_mail_client.twig', $context['form'], 'Uw contactverzoek', $context['form']['email'], '[email protected]');
sendTemplateMail('mail/contact_mail.twig', $context['form'], 'Contactverzoek van '.$context['form']['firstName'].' '.$context['form']['lastName'], '[email protected]', $context['form']['email']);
$context['success'] = 'Uw mail is verstuurd.';
}
endif;
else:
$context['success'] = 'Klik op de Google Recaptcha.';
endif;
else:
$context['success'] = '';
endif;
Timber::render('pages/page-contact.twig', $context );
[/code]
/* Template Name: Contact */
$context = Timber::get_context();
$post = new TimberPost();
$context['post'] = $post;
$context['image'] = get_field('image', $post->ID);
$context['address'] = get_field('address', $post->ID);
$context['address'] = str_replace(' ', '+', $context['address']);
$context['contact_address'] = get_field('contact_address', $post->ID);
$context['phone_number'] = get_field('phone_number', $post->ID);
$context['email'] = get_field('email', $post->ID);
$context['errors'] = [];
if(isset($_POST['send_contact_mail']) && !empty($_POST['send_contact_mail'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'secretcaptchakey';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
//contact form submission code
if(empty($_POST['firstName'])){
$context['errors']['firstName'] = 'Vul uw voornaam in';
} else {
$context['form']['firstName'] = $_POST['firstName'];
}
if(empty($_POST['lastName'])){
$context['errors']['lastName'] = 'Vul uw achternaam in';
} else {
$context['form']['lastName'] = $_POST['lastName'];
}
if(empty($_POST['callName'])){
$context['errors']['callName'] = 'Vul uw roepnaam in';
} else {
$context['form']['callName'] = $_POST['callName'];
}
if(empty($_POST['email'])){
$context['errors']['email'] = 'Vul uw email in';
} else {
$context['form']['email'] = $_POST['email'];
}
if(empty($_POST['comment'])){
$context['errors']['comment'] = 'Vul uw opmerking in';
} else {
$context['form']['comment'] = $_POST['comment'];
}
if(count($context['errors']) == 0){
sendTemplateMail('mail/contact_mail_client.twig', $context['form'], 'Uw contactverzoek', $context['form']['email'], '[email protected]');
sendTemplateMail('mail/contact_mail.twig', $context['form'], 'Contactverzoek van '.$context['form']['firstName'].' '.$context['form']['lastName'], '[email protected]', $context['form']['email']);
$context['success'] = 'Uw mail is verstuurd.';
}
endif;
else:
$context['success'] = 'Klik op de Google Recaptcha.';
endif;
else:
$context['success'] = '';
endif;
Timber::render('pages/page-contact.twig', $context );
[/code]
> Alleen nu ik de validatie code van mijn contact formulier heb moeten omgooien vanwege de capctcha ontvang ik nu geen mails meer:
Wat heeft het een met het ander te maken? :/
Wat gebeurt er dan wel? Of gebeurt er niets? Errorlogs al bekeken? $_POST al gedebugged? Komen de velden send_contact_mail en g-recaptcha-response uberhaupt voor in het formulier?
Speaking of which, waar is de broncode van het formulier?
Wat heeft het een met het ander te maken? :/
Wat gebeurt er dan wel? Of gebeurt er niets? Errorlogs al bekeken? $_POST al gedebugged? Komen de velden send_contact_mail en g-recaptcha-response uberhaupt voor in het formulier?
Speaking of which, waar is de broncode van het formulier?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<div class="col-md-6 section--registration">
<form action="#" method="post">
<div class="row">
<div class="col-md-6">
<input {% if errors.firstName %}class="error"{% endif %} type="text" name="firstName" placeholder="Voornaam" value="{% if form.firstName %}{{ form.firstName }}{% endif %}"><span class="input-error">{% if errors.firstName %}{{ errors.firstName }}{% endif %}</span>
</div>
<div class="col-md-6">
<input {% if errors.lastName %}class="error"{% endif %} type="text" name="lastName" placeholder="Achternaam" value="{% if form.lastName %}{{ form.lastName }}{% endif %}"><span class="input-error">{% if errors.lastName %}{{ errors.lastName }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<input {% if errors.callName %}class="error"{% endif %} type="text" name="callName" placeholder="Roepnaam" value="{% if form.callName %}{{ form.callName }}{% endif %}"><span class="input-error">{% if errors.callName %}{{ errors.callName }}{% endif %}</span>
</div>
<div class="col-md-6">
<input {% if errors.email %}class="error"{% endif %} type="text" name="email" placeholder="E-mailadres" value="{% if form.email %}{{ form.email }}{% endif %}"><span class="input-error">{% if errors.email %}{{ errors.email }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<textarea {% if errors.comment %}class="error"{% endif %} name="comment" placeholder="Opmerkingen">{% if form.comment %}{{ form.comment }}{% endif %}</textarea><span class="input-error">{% if errors.comment %}{{ errors.comment }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="g-recaptcha" data-sitekey="6LfycpUUAAAAAFgYqyewSOh3YFZm576clVoYAjG5"></div>
</div>
</div>
<div class="row">
<button type="submit" name="send_contact_mail" class="btn btn--magenta">
Verzenden</button>
</div>
{% if success %}
<p>{{ success }}</p>
{% endif %}
</form>
</div>
<form action="#" method="post">
<div class="row">
<div class="col-md-6">
<input {% if errors.firstName %}class="error"{% endif %} type="text" name="firstName" placeholder="Voornaam" value="{% if form.firstName %}{{ form.firstName }}{% endif %}"><span class="input-error">{% if errors.firstName %}{{ errors.firstName }}{% endif %}</span>
</div>
<div class="col-md-6">
<input {% if errors.lastName %}class="error"{% endif %} type="text" name="lastName" placeholder="Achternaam" value="{% if form.lastName %}{{ form.lastName }}{% endif %}"><span class="input-error">{% if errors.lastName %}{{ errors.lastName }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-6">
<input {% if errors.callName %}class="error"{% endif %} type="text" name="callName" placeholder="Roepnaam" value="{% if form.callName %}{{ form.callName }}{% endif %}"><span class="input-error">{% if errors.callName %}{{ errors.callName }}{% endif %}</span>
</div>
<div class="col-md-6">
<input {% if errors.email %}class="error"{% endif %} type="text" name="email" placeholder="E-mailadres" value="{% if form.email %}{{ form.email }}{% endif %}"><span class="input-error">{% if errors.email %}{{ errors.email }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<textarea {% if errors.comment %}class="error"{% endif %} name="comment" placeholder="Opmerkingen">{% if form.comment %}{{ form.comment }}{% endif %}</textarea><span class="input-error">{% if errors.comment %}{{ errors.comment }}{% endif %}</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="g-recaptcha" data-sitekey="6LfycpUUAAAAAFgYqyewSOh3YFZm576clVoYAjG5"></div>
</div>
</div>
<div class="row">
<button type="submit" name="send_contact_mail" class="btn btn--magenta">
Verzenden</button>
</div>
{% if success %}
<p>{{ success }}</p>
{% endif %}
</form>
</div>
En wat meldt:
Dan zie je wat er ge-POST wordt.
En mailen is een losstaand proces. Dus als dat niet werkt dan is de kans klein dat het aan de re-Captcha ligt. Enkel aan foute afhandeling.
Dan zie je wat er ge-POST wordt.
En mailen is een losstaand proces. Dus als dat niet werkt dan is de kans klein dat het aan de re-Captcha ligt. Enkel aan foute afhandeling.
Gewijzigd op 05/03/2019 17:39:18 door - Ariën -




