2017-01-02 4 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の統合に関するチュートリアルはオンラインで入手できます。あなたはそれをgoogledしましたか? [tutsplus link](http://smal.me.pn/QLCJ) – Thamilan

+0

私は試しましたが、私は植え付けることができません。私はまだPHPに新しい、私のウェブサイトは他人によって開発された。私は誰かに助けに感謝する、私はこれをスパマーをブロックするために追加する –

+0

@タミランによってリンクされたチュートリアルは、最初のGoogleの結果ですが、実際は全く役に立たず、古いです。少なくとも2つのコメントが便利です。ここでは、[GitHub](https://github.com/google/recaptcha/tree/d3274db7c061770472b8eff8a7dbae0871f6cf03)より詳しい説明があります。私は完全に動作するコードでお答えしたいと思いますが、私もそれに取り組んでいます – Brigo

答えて

0

ここでReCaptcha 2.0を統合する説明。私はちょうど私のウェブサイトでそれをテストし、それは動作します。

  1. が要求here、あなたのHTMLやPHPのコードGitHub reCAPTCHA PHPから
  2. ゴーに統合し、ZIPファイルをダウンロード(またはそれをインストールする手順に従ってくださいする必要があります(パブリックとプライベート)鍵は、私はそれをダウンロードそしてHTML

)私のサーバーにアップロードする

GoogleのreCAPTCHAのAPIを呼び出すために、あなたの <head>タグでこれを挿入します
<script src="https://www.google.com/recaptcha/api.js" async defer></script> 

そして、これはあなたのフォームがPHP

はあなたが

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

そして、最も簡単なコードをダウンロードしたZIPで見つけたファイルautoload.phpを含める

<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 
$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の値は長い英数字の文字列です。

関連する問題