Rumah > Soal Jawab > teks badan
P粉2974349092023-08-28 12:23:43
Walaupun jawapan di sini pasti berkesan, mereka menggunakan GET
请求,这会暴露您的私钥(即使使用 https
). Pada Google Developers, kaedah yang dinyatakan ialah POST< /代码>
.
Untuk butiran lanjut: https://stackoverflow.com/a/323286/1680919
function isValid() { try { $url = 'https://www.google.com/recaptcha/api/siteverify'; $data = ['secret' => '[YOUR SECRET KEY]', 'response' => $_POST['g-recaptcha-response'], 'remoteip' => $_SERVER['REMOTE_ADDR']]; $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data) ] ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return json_decode($result)->success; } catch (Exception $e) { return null; } }
Sintaks tatasusunan: Saya menggunakan sintaks tatasusunan "baharu" ([
和 ]
[ dan ]
bukannya < /代码>array(..)
Nilai pulangantrue
;如果无效,则返回 false
;如果用户有效,则返回 null
如果发生错误。例如,您可以简单地通过编写 if (isValid()) { ... }
: Jika pengguna sah, fungsi ini mengembalikan
P粉2621135692023-08-28 10:59:16
Ini penyelesaiannya
index.html
<html> <head> <title>Google recapcha demo - Codeforgeek</title> <script src='https://www.google.com/recaptcha/api.js'></script> </head> <body> <h1>Google reCAPTHA Demo</h1> <form id="comment_form" action="form.php" method="post"> <input type="email" placeholder="Type your email" size="40"><br><br> <textarea name="comment" rows="8" cols="39"></textarea><br><br> <input type="submit" name="submit" value="Post comment"><br><br> <div class="g-recaptcha" data-sitekey="=== Your site key ==="></div> </form> </body> </html>
verify.php
<?php $email; $comment; $captcha; if(isset($_POST['email'])) $email=$_POST['email']; if(isset($_POST['comment'])) $comment=$_POST['comment']; if(isset($_POST['g-recaptcha-response'])) $captcha=$_POST['g-recaptcha-response']; if(!$captcha){ echo '<h2>Please check the the captcha form.</h2>'; exit; } $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true); if($response['success'] == false) { echo '<h2>You are spammer ! Get the @$%K out</h2>'; } else { echo '<h2>Thanks for posting comment.</h2>'; } ?>
http://codeforgeek.com/2014/12/google-recaptcha-tutorial/