Spam is common to all web sites. Every web developer faces spam and captchas.
Google Recaptcha is one of the best options for getting rid of spam.
In this tutorial, we’ll see how to use Recaptcha in PHP or anything using Google Recaptcha in PHP.
Directory [Hidden]
Register your site and get API keys (Site keys and Secret Keys)
HTML adds Google reCaptcha to the form
Validation with reCAPTCHA
Step 1. Register your website and get the API key (site key and Secret Key)
First, you must register your site with the Google reCaptcha administration Console and get the site key and key.
Tag: the name of your site
ReCatpcha type: Select reCaptcha V2 >> Select the I am not a robot check box.
Domain name: Refer to your website domain name
Submitting it to Google gives you two things
- Site Key
- Secret Key
Copy the Google reCaptcha Site Key and Secret Key for later use in reCaptcha integration code.
Step 2.html adds Google reCaptcha to the form
First, we introduced the reCAPTCHA JavaScript API library. Paste this code before the head closing tag of your HTML template:
<scriptsrc='https://www.google.com/recaptcha/api.js'asyncdefer>Copy the code
in
Finally, paste this code to display the reCAPTCHA component you want and replace the date-sitekey value below with your own Site key.
<div class="g-recaptcha" data-sitekey="your_site_key">Copy the code
For more details, you can refer to the examples below
Example:
<form method="POST">
<input type="text" name="name" value="" /><br>
<input type="text" name="email" value="" /><br>
<textarea name="message"></textarea><br>
<div class="g-recaptcha" data-sitekey="your_site_key"></div><br>
<input type="submit" name="submit" value="SUBMIT">
</form>Copy the code
When you’re done, refresh the page and you’ll see that the reCaptcha component has been added to the form.
Step 3.php code [validate reCAPTCHA]
Replace secret Key with your own.
<? phpif(isset($_POST['submit'])){
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']) {$secretKey = "your_site_key";
$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($response);
if($response->success){
echo "Verification success.";
} else {
echo "Verification failed"; }}}Copy the code
Once you’ve changed $secretKey using your Secret Key, you’re done.
To learn more, please visit:
How to become an architect from a coder
zhuanlan.zhihu.com
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.