-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();
}
}
}
有關各種可在線獲得的有關recaptcha集成的教程。你用Google搜索了嗎?有一個[tutsplus鏈接](http://smal.me.pn/QLCJ) – Thamilan
我試過了,但我不能植入它。即時通訊仍然是新的PHP,我的網站由其他人開發。我感謝有人會幫助,我添加這個來阻止垃圾郵件發送者 –
@Thamilan鏈接的教程是第一個谷歌的結果,但它實際上是無用的和舊的;至少有一些評論是有用的。在[GitHub](https://github.com/google/recaptcha/tree/d3274db7c061770472b8eff8a7dbae0871f6cf03)上可以找到更好的解釋。我想用一個完整的代碼來回答你,但我也在努力 – Brigo