the validation with recaptchat v2 does not work

2

You will not see the validation with recaptcha nose work because the error occurs, I tried to change the code without getting the result

$secretkey = 'Lfi9WAAjjjAFjfgddTmñlokjLU0WT9R';
        $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretkey.'&response='.$recaptcha);
        $responseKeys = json_decode($response);
        if($responseKeys->success)
        {
            return true;
        }
        else
        {
            return false;
        }

Edit - > I add the form

<?php 
    //require_once($_SERVER['DOCUMENT_ROOT'].'/inc/conexion.php');

    if(isset($_POST['login']) && $_POST['login'] == "LogIn"){

        $login = $_POST['username'];
        $password = $_POST['pass'];
        $recaptcha = $_POST["g-recaptcha-response"];

        if(recaptcha($recaptcha)==true){
            $result = $link->query("SELECT * FROM account WHERE login ='".$login."'");
            if($row = mysqli_fetch_object($result)){
                if(password_verify($password, $row->password)){
                    $_SESSION['username'] = $row->login;

                    mysqli_free_result($result);
                    mysqli_close($link);
                }
            }
            else{
                echo 'usuario no encontrado';
                header('Location: ?p=home');

                mysqli_free_result($result);
                mysqli_close($link);
            }
        }
        else{echo 'error! '.$recaptcha;}
    }

    if(!empty($_SESSION['username'])){
        echo '<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">'.$_SESSION['username'].'</a>';

        echo '
            <div class="dropdown-menu log-in">
                <a href="?p=logout"> cerrar sesion</a>
            </div>';
    }
    else{
?>
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Log In</a>
<div class="dropdown-menu log-in">
    <form class="" action="?p=login" method="POST">
        <div class="form-group">
            <input type="text" class="form-control" name="username" placeholder="Username">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" name="pass" placeholder="Password">
        </div>
        <div class"form-group">
            <div class="g-recaptcha captchat" data-theme="dark" data-sitekey="6Lfi9WMUAAAAAFBZ9wyErUktMPOdyruVzwz7z4np"></div>
        </div>
        <div class="form-group form-check">
            <input type="checkbox" class="form-check-input">
            <label class="form-check-label">Recordarme</label>
        </div>
        <input type="submit" name="login" value="LogIn" class="btn btn-signup col-sm-12">
        <a href="#">olvido la contraseña</a>
    </form>
</div>

<?php   
    }
?>
    
asked by crt 02.08.2018 в 00:43
source

1 answer

0

As I commented in the comments, I have simplified it to leave a kind of demo quite simple to try the basics.

You only have to change the keys for the ones you have in Google's api.

  • The first load when not receiving the post returns a message that there is no captcha.
  • If you send without the captcha activated, a captcha error message will be returned.
  • If you send with the captcha activated, it returns an ok message.
  • File recatcha-test.php

    <?php
    error_reporting(E_ALL);
    ini_set('html_errors', 1);
    ini_set('display_startup_errors', 1);
    ini_set("display_errors", 1);
    ini_set('default_charset', 'utf-8');
    
    function recaptcha($recaptcha)
    {
        $secretkey   ='1234567890abcdefghyjklmnop';
        $response    =file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secretkey . '&response=' . $recaptcha);
        $responseKeys=json_decode($response);
        if($responseKeys->success) {
            return true;
        }
        else {
            return false;
        }
    }
    
    $result='Sin valor de captcha';
    if(isset($_POST['g-recaptcha-response'])) {
        $recaptcha=$_POST['g-recaptcha-response'];
    
        if(recaptcha($recaptcha)) {
            $result='Captcha ok!';
        }
        else {
            $result='Captcha error';
        }
    }
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
        <title>Recaptcha</title>
    </head>
    <body>
    
    <h1><?php echo $result; ?></h1>
    
    <form action="" method="POST">
        <div class="g-recaptcha captchat" data-sitekey="abcdefghijk987654321"></div>
        <input type="submit" name="send" value="Enviar">
    </form>
    
    </body>
    </html>
    
        
    answered by 02.08.2018 в 21:01