PHP验证码校验检查了

问题描述 投票:2回答:3

我知道这个问题已经被问过,但我想实现一个验证码成一个简单的接触形式的网站我建立的,但仍无法得到它的工作。该表格可以作为正常预期,但是当我执行的reCAPTCHA按谷歌的指示,如果验证码被选中或不获取表单提交无关。

我的PHP形式的代码如下。

<?php
$action=$_REQUEST['action'];
    {
    $to="[email protected]";
    $name=$_REQUEST['name'];
    $phone=$_REQUEST['phone'];
    $email=$_REQUEST['email'];
    $enquire=$_REQUEST['enquire'];
    $message=$_REQUEST['message'];
    $MESSAGE_BODY = "Name: ".$name."\n";
    $MESSAGE_BODY .= "Phone No: ".$phone."\n";
    $MESSAGE_BODY .= "Email: ".$email."\n";
    $MESSAGE_BODY .= "Enquiring About: ".$enquire."\n";
    $MESSAGE_BODY .= $message;
    $secretKey = "keygoeshere";
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];
    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($responses);
    if ($response->success)
        {
        $from="From: $name <$email>\r\nReturn-path: $email";
        $subject="Message from $name about $enquire";
        mail($to, $subject, $MESSAGE_BODY, $from);
        header('Location: /sent.php');
        }
    else{
        echo "All * fields are required, please fill out <a href=\"../contact.php\">the form</a> again.";
        }
    }  
?>
php submit recaptcha verify
3个回答
0
投票

这里是我的验证码的代码,在我的网站工作。

Kohana的下方2.3 Framework代码,您可以编辑和使用计划的PHP格式填写

<?php

if($_POST){

    $this->userPost = $this->input->post();
    $post = new Validation($_POST);
    $post = Validation::factory(array_merge($_POST))
                ->pre_filter('trim')
                ->add_rules('name', 'required')
                ->add_rules('email','required','valid::email')
                ->add_rules('message', 'required')
                ->add_rules('g-recaptcha-response', 'required');

    $captcha = $this->input->post('g-recaptcha-response');
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET-KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);

    $obj = json_decode($response);

    if($obj->{'success'}==false)
    {
        $this->form_error['name'] = '*Please Fill the Name';
        $this->form_error['email'] = '*Please Fill the Email';
        $this->form_error['message'] = '*Please Fill the Message';
        $this->form_error['captcha_code'] = '*Are you a bot!';

    }elseif($post->validate()){


        $status = $this->home->mob_app(arr::to_object($this->userPost));

            if($status != 0){

                if(isset($_POST['message'])) { $feedback= $_POST['message']; } else { $feedback='-'; } 
                $name=$_POST['name'];
                $leadid= 'LE-'.$status;
                $subject = "Reg : ".$leadid." - Inquiry";
                $txts = '<h4>Lead Details :</h4></br>
                    <p><b>Name : </b> '.$name.'</p></br>
                    <p><b>From : </b> '.$_POST['email'].'</p></br>
                    <p><b>Description :</b> '.$feedback.'</p>';
                $from = $_POST['email'];
                $to="[email protected]";                                                   
                email::sendgridnew($from, $to, $subject, $txts);
                url::redirect(PATH.'thankyou.html');
            }
        }else{

            $this->form_error = error::_error($post->errors());
        }

    }
    $this->captchastring = '';
    for ($i = 0; $i < 5; $i++) {
    $this->captchastring .= chr(rand(97, 122));
}

?>

0
投票

或许这只是在后,但你有“$回应”,这似乎是一个错字。

此外,您还可以尝试转储价值$回应,看到什么值,如果成功,是无效的。

或者你可以用POST(验证您的POST值)组合使用卷曲:


        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 
                http_build_query(
                    array(
                        'secret' => 'your-secret',
                        'response' => $_POST['g-recaptcha-response'],
                        'remoteip' => $_SERVER['REMOTE_ADDR']
                    )
                )
        );

        // receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec ($ch);

        curl_close ($ch);

        $recaptcha_result = json_decode($server_output, true);

        if(!empty($recaptcha_result['success'])) {
            // etc
        }

0
投票
<?php

$action=$_REQUEST['action'];
    {

    $to="[email protected]";
    $name=$_REQUEST['name'];
    $phone=$_REQUEST['phone'];
    $email=$_REQUEST['email'];
    $enquire=$_REQUEST['enquire'];
    $message=$_REQUEST['message'];

    $MESSAGE_BODY = "Name: ".$name."\n";
    $MESSAGE_BODY .= "Phone No: ".$phone."\n";
    $MESSAGE_BODY .= "Email: ".$email."\n";
    $MESSAGE_BODY .= "Enquiring About: ".$enquire."\n";
    $MESSAGE_BODY .= $message;

    $secretKey = "keygoeshere";
    $responseKey = $_POST['g-recaptcha-response'];
    $userIP = $_SERVER['REMOTE_ADDR'];

 $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";

    $response = file_get_contents($url);
    $response = json_decode($responses);
    if ($response->success)
        {

       $from="From: $name <$email>\r\nReturn-path: $email";
        $subject="Message from $name about $enquire";
        mail($to, $subject, $MESSAGE_BODY, $from);
        header('Location: /sent.php');
        }
    else
        {
        echo "All * fields are required, please fill out <a href=\"../contact.php\">the form</a> again.";
        }
    }  
?>
© www.soinside.com 2019 - 2024. All rights reserved.