2017-01-02 61 views
-1

有人可以幫我添加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

回答

0

這裏是整合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的值是一個長的字母數字字符串。