需要实施recaptcha

问题描述:

有人可以帮我添加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){ 
    $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(); 
    } 

} 

} 
+0

有关各种可在线获得的有关recaptcha集成的教程。你用Google搜索了吗?有一个[tutsplus链接](http://smal.me.pn/QLCJ) – Thamilan

+0

我试过了,但我不能植入它。即时通讯仍然是新的PHP,我的网站由其他人开发。我感谢有人会帮助,我添加这个来阻止垃圾邮件发送者 –

+0

@Thamilan链接的教程是第一个谷歌的结果,但它实际上是无用的和旧的;至少有一些评论是有用的。在[GitHub](https://github.com/google/recaptcha/tree/d3274db7c061770472b8eff8a7dbae0871f6cf03)上可以找到更好的解释。我想用一个完整的代码来回答你,但我也在努力 – Brigo

这里是整合ReCaptcha 2.0的说明。我只是在我的网站上测试它,它的工作原理。

  1. 请求here你需要在你的HTML和PHP代码
  2. 转到GitHub reCAPTCHA PHP整合和下载ZIP文件(或按照说明安装密钥(公钥和私钥);我下载并上载我的服务器上)

HTML

插入这在<head>标签调用谷歌的reCAPTCHA API

<script src="https://www.google.com/recaptcha/api.js" async defer></script> 

这该是多么的形式出现

<form action="..." method="POST"> 
    _list of your inputs_ 

    <div class="g-recaptcha" data-sitekey="your_site_key(the_public_one)"></div> 
    <input type="submit" value="Submit"> 
</form> 

PHP

包括您在您下载

<?php require('path_where_you_uploaded_the_folder/recaptcha/src/autoload.php'); ?> 

的ZIP找到该文件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 
} 

?> 

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