Ví dụ đăng nhập cơ bản bằng PHP kết hợp reCaptcha
- Để bắt đầu, bạn cần phải đăng ký SiteKey và SecretKey từ https://www.google.com/recaptcha- Đăng ký:
Sau khi đăng ký, bạn sẽ nhận được Google Site Key và Google Secret Key
- Google Site Key: bạn sẽ sử dụng mã này cho trang HTML
- Google Secret Key: mã này sẽ giúp trang web của bạn tiếp xúc với Google
- Tiếp theo, tạo một trang HTML đơn giản như sau index.html (thay đổi data-sitekey của riêng bạn)
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Google reCaptcha v2 - Demo Login</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<form class="form-signin" role="form" action="validateform.php" method="POST">
<div id="status"></div>
<h2 class="form-signin-heading">Đăng nhập</h2>
<label for="inputEmail" class="sr-only">Username</label>
<input type="text" name="txtuser" id="inputEmail" value="" class="form-control" placeholder="Tên đăng nhập" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="txtpass" id="inputPassword" value="" class="form-control" placeholder="Mật khẩu" required>
<div class="g-recaptcha" data-sitekey="6LcnV_8SAAAAAGBwDp38k-tY1c9sugh27MJqPuO4"></div>
<button class="btn btn-lg btn-primary btn-block" type="submit" name="ok">Đăng nhập</button>
</form>
<div style=" text-align:center;font-size:12px;margin-top:20px"><span class="msg">Chú ý:</span> Tên đăng nhập: <strong>nhatchanh.info</strong> | Mật khẩu: <strong>admin</strong></div>
</div> <!-- /container -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</body>
</html>
Nếu bạn muốn sử dụng theme dark và xác minh reCaptcha bằng Audio thì bạn thay đoạn <div>...</div> trên thành
<div class="g-recaptcha"
data-sitekey="6LcnV_8SAAAAAGBwDp38k-tY1c9sugh27MJqPuO4" data-theme="dark" data-theme="dark" data-type="audio"></div>
- Tạo một trang validateform.php để kiểm tra các giá trị đầu vào: (thay đổi $secret_key của riêng bạn)
<?phpChúc thành công!
//@author = nhatchanh.info
require_once "recaptchalib.php";
// reCAPTCHA hỗ trợ hơn 40 ngôn ngữ, xem tại: https://developers.google.com/recaptcha/docs/language
$lang = "en";
// Phản ứng từ reCAPTCHA
$resp = null;
// Thông báo lỗi từ reCAPTCHA nếu có
$error = null;
$secret_key = "6LcnV_8SAAAAAJB-F7XM3l9JUQF3s8SrDxuTjN5E"; //Thay đổi secret_key của bạn - https://www.google.com/recaptcha/admin
$reCaptcha = new ReCaptcha($secret_key);
if(isset($_POST['ok'])){
if($_POST['txtuser'] != "nhatchanh.info"){
echo "<p style='color:#FF0000'>Tên đăng nhập không đúng.</p>";
}else{
$u=$_POST['txtuser'];
}
if($_POST['txtpass'] != "admin"){
echo "<p style='color:#FF0000'>Mật khẩu không đúng.</p>";
}else{
$p=$_POST['txtuser'];
}
if ($_POST["g-recaptcha-response"]) {
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
//print_r($resp);
} else {
echo "<p style='color:#FF0000'>Bạn chưa xác nhận Recaptcha</p>";
}
if ($resp != null && $resp->success && $u && $p) {
echo "<p style='color:#0099FF'>Bạn đả đăng nhập thành công!</p>";
//Hiển thị thông tin khi đăng nhập thành công
echo "Tên đăng nhập: <a href='http://".$u."'>".$u."</a>";
}
}
?>
Post a Comment