G
G
Gares Antonovich2017-05-13 01:13:59
PHP
Gares Antonovich, 2017-05-13 01:13:59

Google recaptcha sends without confirmation, why?

Please tell me there was a contact form with another captcha, it was cut out, everything works fine.
I connected the recaptcha from the client side, everything also works fine
. But now it doesn’t work with the server to get it working.

Contact Form Submission Handler Spoiler
$error = array();



$name = $_POST['name'];

$phone = $_POST['phone'];

$email = $_POST['email'];

$clinic = $_POST['clinic'];

$daypart = $_POST['daypart'];

$date = $_POST['date'];

$comment = $_POST['comment'];


if(!$name){
    $error['name'] = 'Пожалуйста, укажите свое имя';
}
if(!$phone){

    $error['phone'] = 'Пожалуйста, укажите свой контактный номер телефона';

}elseif(!preg_match('/^[0-9 \+\-]+$/',$phone)){

    $error['phone'] = 'Неверный формат номера телефона, можно использовать только цифры "+" и "-"';
}elseif(strlen(preg_replace('/^[^0-9]$/','',$phone)) < 7){
    $error['phone'] = 'Номер телефона должен содержать не менее 7-ми цифр';
}

if($email !== null){
    if(!$email){
        $error['email'] = 'Пожалуйста, укажите свой контактный email';
    }elseif(!preg_match('#^((http(s)?|ftp)://)?[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$#i',$email)){
        $error['email'] = 'Неверный формат email';
    }
}
if($clinic !== null){
    if(!$clinic){
        $error['clinic'] = 'Пожалуйста, укажите клинику';
    }
}
if($daypart !== null){
    if(!$daypart){
        $error['daypart'] = 'Пожалуйста, укажите в какой половине дня удобнее';
    }
}
if($date !== null){
    if(!$date){
        $error['date'] = 'Пожалуйста, укажите клинику';
    }
}

if(!$comment){
    $error['comment'] = 'Пожалуйста, опишите Ваш вопрос';
}

if ($error) {
    $result = array('validation' => $error);
} else {
    if ($email === null) {
        $body = "Имя: " . $name . "
Номер телефона: " . $phone . "
Клиника: " . $clinic . "
Время: " . $daypart . "
Дата: " . $date . "
Вопрос: " . $comment;
        $subject = 'Форма "Запись на прием", ' . $name;
    } else {
        $body = "Имя: " . $name . "
Номер телефона: " . $phone . "
email: " . $email . "
Вопрос: " . $comment;

        $subject = 'Форма "Вы можете в любую минуту написать нам", ' . $name;
    }

    if (!mail(EMAIL, $subject, $body, 'Content-type: text/plain; charset=utf-8')) {
        $result = false;
    }
}

echo json_encode($result);
Contact form itself Spoiler
<div class="register-form">
          <form class="row white block form-inline front-form _appointments-form">

<div class="col-md-6 col-sm-6">
              <input class="fullwidth form-control input-lg" type="text" name="name" value="" placeholder="Ваше имя *" />
              <input class="fullwidth form-control input-lg" type="text" name="phone" value="" placeholder="Номер телефона *" />
              

              <div class="row select-clinic">
                <div class="col-sm-8">
                  <select class="fullwidth form-control input-lg" name="clinic">
                    <option value="">&nbsp;&nbsp;&nbsp;lorem</option>
                    <option>&nbsp;&nbsp;&nbsp;lorem</option>
                    <option>&nbsp;&nbsp;&nbsp;lorem</option>
                  </select>
                </div>
                <div class="col-sm-4">
                  <select class="fullwidth form-control input-lg" name="date">
                    <option value="">&nbsp;&nbsp;Дата</option>
                    {foreach from=$dates item=date}
                      <option value="{$date}">&nbsp;&nbsp;{$date}</option>
                    {/foreach}
                  </select>
                </div>
                <div class="col-sm-12">
                  <select class="fullwidth form-control input-lg" name="daypart">
                    <option value="">&nbsp;&nbsp;&nbsp;lorem</option>										
                    <option>&nbsp;&nbsp;&nbsp;В Первой</option>
                    <option>&nbsp;&nbsp;&nbsp;Во Второй</option>
                  </select>
                </div>
              </div>

                
            </div>

            <div class="col-md-6 col-sm-6">
              <textarea class="fullwidth form-control input-lg" rows="7" name="comment" placeholder="Коментарий *"></textarea>
            </div>

            <div class="col-md-12 text-center">

<div class="g-recaptcha" data-sitekey="6Lca9iAUAAAAAP3OGtiy44SSdgbSrRpXccjZueGl"></div>
              <button type="submit" name="enter" class="btn btn-primary btn-register" onclick="ga('send', 'event', 'Write form', 'send', 'main', { nonInteraction: true });"><i class="fa fa-stethoscope fa-2x"></i> Записаться на прием</button>
            </div>

          </form>
        </div>
Recaptcha check handler based on the Spoiler tutorial
if($_SERVER['REQUEST_METHOD'] == 'POST'){


  if(empty($_POST['g-recaptcha-response'])){

    $recaptcha = $_POST['g-recaptcha-response'];

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $secret = 'туть секретный ключ';
    $ip = $_SERVER['REMOTE_ADDR'];
    $url_data = $url.'?secret='.$secret.'&response='.$recaptcha.'&remoteip='.$ip;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$url_data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

    $res = curl_exec($curl);
    curl_close($curl)
    $res = json_decode($res);
    if($res-> sucsess){

    }else{
  }
}

Can't figure out what to do next or what am I doing wrong?
Sending a Server Internal Error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Teterin, 2017-05-13
@Rupaka

You have banal syntax errors in the code "Recaptcha check handler which is written according to the Spoiler lesson"
curl_close($curl) - there is no semicolon at the end.
if($_SERVER['REQUEST_METHOD'] == 'POST'){ - no closing bracket.
You need to use a normal IDE (PHPStorm) that will immediately underline your jambs in red, and you do not need to spend so much time on such banal errors. I just pasted your code into PHPStorm and it showed all the problem areas ca3c68af-8e58-4731-ac9a-b3c21744c4de.png
and be sure to read PSR-1 PSR-2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question