Página 1 de 2

[Resolvido] Erro PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 01:23
por rlasmar
Opencart 2.0.3.1 Tema Default.

Várias vezes ao dia aparece esse erro abaixo no log, referente ao recaptcha do google no formulário de contato. Eu nunca consegui reproduzir esse erro, então não sei como ocorre, suspeito que seja spam que não consegue resolver o desafio do google, pois o formulário funciona normalmente para clientes que realmente tenham dúvida e mandam mensagem.
Código: Selecionar todos
PHP Notice:  Undefined index: g-recaptcha-response in /home/admin/web/site/public_html/system/modification/catalog/controller/information/contact.php on line 237
A linha 237 tem o seguinte código:
Código: Selecionar todos
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $this->request->post['g-recaptcha-response'] . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

			$recaptcha = json_decode($recaptcha, true);

			if (!$recaptcha['success']) {
				$this->error['captcha'] = $this->language->get('error_captcha');
			}
		}
Alguém sabe como resolver?

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 02:31
por Manoel Vidal
Olá @rlasmar.

Você está correto em sua suspeita, sem dúvidas é SPAM.

Para resolver o problema no arquivo:
catalog/controller/information/contact.php

Modifique a linha abaixo:
Código: Selecionar todos
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $this->request->post['g-recaptcha-response'] . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

Pelas linhas abaixo:
Código: Selecionar todos
$g-recaptcha-response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g-recaptcha-response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

Salve as alterações no arquivo e atualize o cache de modificações.

Espero ter ajudado. :)

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 12:10
por rlasmar
Não deu certo Manoel, dá erro de código
Código: Selecionar todos
Parse error: syntax error, unexpected '=' in /home/admin/web/site/public_html/system/modification/catalog/controller/information/contact.php on line 238

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 14:05
por Manoel Vidal
Fiz mais uma correção no código para resolver o problema. :D

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 15:47
por rlasmar
Manoel Vidal escreveu:Fiz mais uma correção no código para resolver o problema. :D
Não deu certo ainda, continua o mesmo erro de syntax

Parse error: syntax error, unexpected '=' in /home/admin/web/site/public_html/system/modification/catalog/controller/information/contact.php on line 238

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 16:37
por Manoel Vidal
Você adicionou novamente o código no arquivo?
Se sim, posteriormente atualizou novamente o cache de modificações?

Testei aqui e o erro citado não acontece. :)

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 16:42
por rlasmar
Manoel Vidal escreveu:Você adicionou novamente o código no arquivo?
Se sim, posteriormente atualizou novamente o cache de modificações?

Testei aqui e o erro citado não acontece. :)
Sim, limpei o cache.

Aqui está o código completo
Código: Selecionar todos
if ($this->config->get('config_google_captcha_status')) {
			$g-recaptcha-response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
			$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g-recaptcha-response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);
			$recaptcha = json_decode($recaptcha, true);

			if (!$recaptcha['success']) {
				$this->error['captcha'] = $this->language->get('error_captcha');
			}
		}

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 13 Fev 2019, 20:04
por Manoel Vidal
Como você está fazendo o teste?

Pois ao enviar o formulário de contato no OpenCart?

Não estou conseguindo replicar o erro que você mencionou depois da modificação no código.

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 14 Fev 2019, 09:01
por veteranodf
Tem um erro no nome da variável "$g-recaptcha-response", deve substituir os hífens por underlines;

Substitua:
Código: Selecionar todos
$g-recaptcha-response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g-recaptcha-response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

Por:
Código: Selecionar todos
$g_recaptcha_response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g_recaptcha_response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

Re: [ERRO] PHP Notice: Undefined index: g-recaptcha-response

Enviado: 14 Fev 2019, 12:36
por rlasmar
Manoel Vidal escreveu:Como você está fazendo o teste?

Pois ao enviar o formulário de contato no OpenCart?

Não estou conseguindo replicar o erro que você mencionou depois da modificação no código.
Não chega nem a abrir a página de contato, já mostra o erro:
Parse error: syntax error, unexpected '=' in /home/admin/web/site/public_html/system/modification/catalog/controller/information/contact.php on line 238

Com o código do vetereno aparentemente deu certo, tinha que trocar o hifen por underline. Vou aguardar as próximas horas para ver se não ocorre mais erros quando há SPAM.
veteranodf escreveu:Tem um erro no nome da variável "$g-recaptcha-response", deve substituir os hífens por underlines;

Substitua:
Código: Selecionar todos
$g-recaptcha-response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g-recaptcha-response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);

Por:
Código: Selecionar todos
$g_recaptcha_response = (isset($this->request->post['g-recaptcha-response'])) ? $this->request->post['g-recaptcha-response'] : 'null';
$recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($this->config->get('config_google_captcha_secret')) . '&response=' . $g_recaptcha_response . '&remoteip=' . $this->request->server['REMOTE_ADDR']);
Aparentemente deu certo, o erro de syntaxe sumiu, agora vou esperar alguns dias para ver se ocorre ainda erro com relação aos SPAM.