Added Recaptcha

This commit is contained in:
CodeServer
2021-07-31 07:19:40 +01:00
parent 118ada3aee
commit 0d3fee6ba2
9 changed files with 152 additions and 40 deletions
+23 -26
View File
@@ -24,48 +24,45 @@ $result = query_username($conn, $userName);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$passWord_hashed = $row["C_Password"];
$passWord_correct = password_verify($passWord_user, $passWord_hashed);
if ($passWord_correct === false) {
echo "<script> alert('Wrong password!');location.href='../index.html'; </script>";
echo "<script> alert('Wrong password!');location.href='../index.php'; </script>";
exit();
} else {
// if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// $secretKey = "6LehX_4aAAAAANIoyIRIYn8QzZtwtE7ytaQ1hgmZ";
// $responseKey = $_POST['g-recaptcha-response'];
// $userIP = $_SERVER['REMOTE_ADDR'];
// $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
// $response = file_get_contents($url);
// $response = json_decode($response);
$secretKey = "6LdDrc8bAAAAAGhP99aDPJ2_6O7yrwIUc8yJLaQU";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
$response = json_decode($response);
// if($response->success){
// echo "Verification success.";
// $_SESSION['username'] = $userName;
// header("Location: shopping.php");
// } else {
// echo "<script> alert('reCAPTHCA verification failed, please try again.');location.href='login.php'; </script>";
// return;
// }
if($response->success){
echo "Verification success.";
$_SESSION['username'] = $userName;
header("Location: ../shopping/shopping.php");
} else {
echo "<script> alert('reCAPTHCA verification failed, please try again.');location.href='login.php'; </script>";
return;
}
// } else {
// echo "<script> alert('Please click reCAPTHCA to verify.');location.href='login.php'; </script>";
// return;
// }
} else {
echo "<script> alert('Please click reCAPTHCA to verify.');location.href='../index.php'; </script>";
return;
}
// TODO: delete later
echo "Verification success.";
$_SESSION['username'] = $userName;
header("Location: ../shopping/shopping.php");
}
} else {
// echo "<script> alert('Username dosen't exist.Please sign up first.');location.href='../index.html'; </script>";
header("Location: ../index.html");
// echo "<script> alert('Username dosen't exist.Please sign up first.');location.href='../index.php'; </script>";
header("Location: ../index.php");
exit();
}
+59
View File
@@ -0,0 +1,59 @@
<?php
/*header("Access-Control-Allow-Origin: https://www.gstatic.com");*/
/*if(isset($_POST['submit']) && !empty($_POST['submit'])){
// check do we have recaptcha param added to form and submited
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
//your site secret recaptcha key
$secret = '6LehX_4aAAAAANIoyIRIYn8QzZtwtE7ytaQ1hgmZ';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
// check was the response successfully checked by Google
if($responseData->success){
// if recaptcha check was success
$succMsg = 'Your contact request have submitted successfully.';
exit($succMsg);
}else{
// if not show the error
$errMsg = 'Robot verification failed, please try again.';
echo "<script type='text/javascript'>alert('$errMsg');</script>";
}
}else{
// if recaptcha is not checked
$errMsg = 'Please click on the reCAPTCHA box.';
echo "<script type='text/javascript'>alert('$errMsg');</script>";
}
} */
if(isset($_POST['submit'])) {
if(!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
echo 'reCAPTHCA verification failed, please try again.';
} else {
$secret = 'google_secret_key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->success) {
// What happens when the CAPTCHA was entered incorrectly
echo 'Successful login.';
} else {
// Your code here to handle a successful verification
echo 'reCAPTHCA verification failed, please try again.';
}
}
}
?>
+1 -1
View File
@@ -8,7 +8,7 @@ $passWord = $_POST['password'];
$query = "INSERT INTO account(C_ID,C_Password) VALUES('$userName','$passWord')";
// if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
// $secretKey = "6LehX_4aAAAAANIoyIRIYn8QzZtwtE7ytaQ1hgmZ";
// $secretKey = "6LdDrc8bAAAAAGhP99aDPJ2_6O7yrwIUc8yJLaQU";
// $responseKey = $_POST['g-recaptcha-response'];
// $userIP = $_SERVER['REMOTE_ADDR'];
// $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";