Php 需要实施recaptcha

Php 需要实施recaptcha,php,recaptcha,Php,Recaptcha,有人能帮我在这个代码上添加recaptcha吗 这是我的php注册表 //if form has been submitted process it if(isset($_POST['submit'])){ //very basic validation if($_POST['username'] == ''){ $error[] = 'Username is required.'; }else if(strlen($_POST['username']) < 6){ $e

有人能帮我在这个代码上添加recaptcha吗

这是我的php注册表

//if form has been submitted process it
if(isset($_POST['submit'])){

//very basic validation
if($_POST['username'] == ''){
    $error[] = 'Username is required.';
}else if(strlen($_POST['username']) < 6){
    $error[] = 'Username is too short. (6 Chars)';
}else if(strlen($_POST['username']) > 32){
    $error[] = 'Username is too long. (32 Chars)';
}else if(preg_match('/[^a-z0-9_]/', $_POST['username'])){
    $error[] = 'Only a-z, 0-1 and _ are allowed in username.';
} else {
    $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
    $stmt->execute(array(':username' => $_POST['username']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!empty($row['username'])){
        $error[] = 'Username provided is already in use.';
    }

}

//email validation
 if($_POST['email'] == ''){
    $error[] = 'Email Address is required.';
}else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
    $error[] = 'Please enter a valid Email Address';
} else {
    $stmt = $db->prepare('SELECT email FROM members WHERE email = :email');
    $stmt->execute(array(':email' => $_POST['email']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!empty($row['email'])){
        $error[] = 'Email Address provided is already in use.';
    }

}

 if($_POST['mobile'] == ''){
    $error[] = 'Mobile Number is required.';
}else if(!is_numeric($_POST['mobile'])){
    $error[] = 'Mobile Number should be numeric.';
}else if(strlen($_POST['mobile']) < 10){
    $error[] = 'Mobile Number is too short.';
}
else if(strlen($_POST['mobile']) > 10){
    $error[] = 'Mobile Number is too long.';
} else {
    $stmt = $db->prepare('SELECT mobile FROM members WHERE mobile = :mobile');
    $stmt->execute(array(':mobile' => $_POST['mobile']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!empty($row['mobile'])){
        $error[] = 'Mobile Number is already in use.';
    }
}   

if($_POST['password'] == ''){
    $error[] = 'Password is required.';
}else if(strlen($_POST['password']) < 6){
    $error[] = 'Password is too short. (6 Chars)';
}else if(strlen($_POST['passwordConfirm']) < 6){
    $error[] = 'Confirm password was too short. (6 Chars)';
}else if($_POST['password'] != $_POST['passwordConfirm']){
    $error[] = 'Passwords do not match.';
}

//if no errors have been created carry on
if(!isset($error)){

    //hash the password
    $hashedpassword = $user->password_hash($_POST['password'], PASSWORD_BCRYPT);

    //create the activasion code
    $activation = md5(uniqid(rand(),true));


    $usrname = str_replace(' ', '', $_POST['username']);
    $usrname = preg_replace('/\s+/','',$_POST['username']);
    try {

        //insert into database with a prepared statement
        $stmt = $db->prepare('INSERT INTO members (username,password,email,mobile,active) VALUES (:username, :password, :email, :mobile, :active)');
        $stmt->execute(array(
            ':username' => strtolower($usrname),
            ':password' => $hashedpassword,
            ':email' => $_POST['email'],
            ':mobile' => $_POST['mobile'],
            ':active' => $activation
        ));

        header('Location: register.php?action=joined');
        exit;

    //else catch the exception and show the error.
    } catch(PDOException $e) {
        $error[] = $e->getMessage();
    }

}

}
//如果表单已提交,请对其进行处理
如果(isset($_POST['submit'])){
//非常基本的验证
如果($_POST['username']=''){
$error[]=“需要用户名”;
}else if(strlen($_POST['username'])<6){
$error[]=“用户名太短。(6个字符)”;
}else if(strlen($_POST['username'])>32){
$error[]=“用户名太长。(32个字符)”;
}else if(preg_match('/[^a-z0-9.]/',$_POST['username'])){
$error[]=“用户名中只允许a-z、0-1和uu”;
}否则{
$stmt=$db->prepare('SELECT username FROM members,其中username=:username');
$stmt->execute(数组(':username'=>$\u POST['username']);
$row=$stmt->fetch(PDO::fetch_ASSOC);
如果(!empty($row['username'])){
$error[]=“提供的用户名已在使用中。”;
}
}
//电子邮件验证
如果($_POST['email']=''){
$error[]='需要电子邮件地址';
}否则如果(!filter_var($_POST['email'],filter_VALIDATE_email)){
$error[]=“请输入有效的电子邮件地址”;
}否则{
$stmt=$db->prepare('SELECT email FROM members,其中email=:email');
$stmt->execute(数组(':email'=>$\u POST['email']);
$row=$stmt->fetch(PDO::fetch_ASSOC);
如果(!empty($row['email'])){
$error[]=“提供的电子邮件地址已在使用中。”;
}
}
如果($_POST['mobile']=''){
$error[]=“需要手机号码”;
}如果(!是数字($\u POST['mobile']),则为else{
$error[]=“手机号码应该是数字。”;
}else if(strlen($_POST['mobile'])<10){
$error[]=“手机号码太短。”;
}
else if(strlen($_POST['mobile'])>10){
$error[]=“手机号码太长。”;
}否则{
$stmt=$db->prepare('SELECT mobile FROM members,其中mobile=:mobile');
$stmt->execute(数组(':mobile'=>$\u POST['mobile']);
$row=$stmt->fetch(PDO::fetch_ASSOC);
如果(!empty($row['mobile'])){
$error[]=“手机号码已在使用中。”;
}
}   
如果($_POST['password']=''){
$error[]='需要密码';
}else if(strlen($_POST['password'])<6){
$error[]=“密码太短。(6个字符)”;
}else if(strlen($_POST['passwordConfirm'])<6){
$error[]=“确认密码太短。(6个字符)”;
}else if($\u POST['password']!=$\u POST['passwordConfirm'])){
$error[]='密码不匹配';
}
//如果未创建错误,请继续
如果(!isset($error)){
//散列密码
$hashedpassword=$user->password\u散列($\u POST['password'],password\u BCRYPT);
//创建激活代码
$activation=md5(uniqid(rand(),true));
$usrname=str_replace(“”,,$_POST['username']);
$usrname=preg_replace('/\s+/','',$\u POST['username']);
试一试{
//使用准备好的语句插入数据库
$stmt=$db->prepare('插入成员(用户名、密码、电子邮件、手机、活动)值(:用户名、:密码、:电子邮件、:手机、:活动)');
$stmt->execute(数组)(
':username'=>strtolower($usrname),
':password'=>$hashedpassword,
“:email'=>$\u POST['email'],
“:mobile'=>$\u POST['mobile'],
“:活动”=>$activation
));
标题('Location:register.php?action=joined');
出口
//否则捕获异常并显示错误。
}捕获(PDO$e){
$error[]=$e->getMessage();
}
}
}

这里是集成ReCaptcha 2.0的说明。我刚刚在我的网站上测试了一下,效果不错

  • 请求需要集成到HTML和PHP代码中的密钥(公共和私有)
  • 转到并下载ZIP文件(或按照说明安装;我下载并上传到我的服务器上)
  • HTML 在
    标记中插入此项以调用Google reCAPTCHA API

    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
    
    执行检查的最简单代码是:

    <?php
    $siteKey = 'your_public_key'; //ex. 6OfGWERRRRt17YkojJGk2mEeM8fgEPKSpiPe
    $secret = 'your_private_key';
    $recaptcha = new \ReCaptcha\ReCaptcha($secret);
    $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); //the values for: $gRecaptchaResponse, $remoteIp
    if ($resp->isSuccess()) {
        echo 'GREAT!'; //insert here the code you'll want to process if the verification is ok or the value you want to return (if this code is inserted in a function)
    } else {
        $errors = $resp->getErrorCodes();
        echo 'NOOPE..'; //print_r($errors): you'll see which is/are the error
    }
    
    ?>
    
    
    

    表单将属性
    g-recaptcha-response
    传递给PHP脚本;如果你
    print\r($\u POST['g-recaptcha-response']
    或者你
    print\r($\u POST)
    你会看到如果检查结果是肯定的(你没有被标记为机器人),g-recaptcha-response的值是一个很长的字母数字字符串。

    网上有很多关于recaptcha集成的教程。你用谷歌搜索过吗?有一个我试过了,但我无法植入它。我对php还是新手,我的网站是由别人开发的。我很感谢有人能帮我,我加上这个来阻止垃圾邮件制造者由@Thamilan链接的教程是第一个谷歌结果,但它实际上非常无用和陈旧;至少有几条评论是有用的。在这里,你可以找到更好的解释。我想用一个完整的代码来回答你,但我也在努力
    <?php require('path_where_you_uploaded_the_folder/recaptcha/src/autoload.php'); ?>
    
    <?php
    $siteKey = 'your_public_key'; //ex. 6OfGWERRRRt17YkojJGk2mEeM8fgEPKSpiPe
    $secret = 'your_private_key';
    $recaptcha = new \ReCaptcha\ReCaptcha($secret);
    $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); //the values for: $gRecaptchaResponse, $remoteIp
    if ($resp->isSuccess()) {
        echo 'GREAT!'; //insert here the code you'll want to process if the verification is ok or the value you want to return (if this code is inserted in a function)
    } else {
        $errors = $resp->getErrorCodes();
        echo 'NOOPE..'; //print_r($errors): you'll see which is/are the error
    }
    
    ?>