难倒了 recaptcha 验证步骤
stumped with recaptcha verification step
我讨厌寻求帮助.. 但我不知道花了多少时间来弄清楚如何使用 recaptcha 为这个表单实施验证步骤。
我知道我需要在某处定义私钥,但大多数其他指南都在使用 "isset" 函数,我只是不知道在 php 的哪个部分执行此操作。
目前,该按钮显示出来并且可以选中。但是当我点击提交时,无论该框是否被选中,它都会发送。 (只是解释以防万一。)
谢谢,抱歉。
<?php
$to = 'example@caraudioinc.com';
$subject = 'CarAudioInc.com Form Message';
$errName = $errEmail = $errMessage = "";
$name = $email = $pNum = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$pNum = test_input($_POST["pNum"]);
$message = test_input($_POST["message"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errName = "*Name field is required.";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Invaild name.";
}
}
if (empty($_POST["email"])) {
$errEmail = "*Email field is required.";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid E-mail.";
}
}
if (empty($_POST["message"])) {
$errMessage = "*Message field is required.";
} else {
$message = test_input($_POST["message"]);
}
$body = <<<EMAIL
Message from: $name
$message
Phone: $pNum
Email: $email
EMAIL;
$header = "From: $email";
if (!$errName && !$errEmail && !$errMessage ) {
if($_POST) {
mail($to, $subject, $body, $header);
header('Location: /sent');
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=1152">
<script src="https://www.google.com/recaptcha/api.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-100617570-1" ></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-100617570-1');
</script>
<!--Google meta tags -->
<title>Car Audio Inc | Contact</title>
<!--fb meta tags -->
<link rel="canonical" href="https://www.caraudioinc.com/contact" />
<link href="css/multiColumnTemplate.css" hreflang="en" rel="stylesheet" type="text/css">
<link rel="icon" href="images/favicon.png" type="image/png">
</head>
<body>
<div class="container">
<header>
<div class="primary_header">
<nav class="secondary_header" id="menu">
<ul>
<li><a href="/">HOME</a></li>
<li><a href="about">ABOUT</a></li>
<li><a href="services">SERVICES</a></li>
<li class="current"><a href="contact">CONTACT</a></li>
</ul>
</nav>
</header>
<div class="contactForm">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"><br>
<label for="name">Name </label><span class="error"><?php echo $errName; ?></span><br>
<input type="text" id="name" name="name" value="<?php echo $name;?>"><br><br>
<label for="email">Email </label><span class="error"><?php echo $errEmail; ?></span><br>
<input type="email" id="email" name="email" value="<?php echo $email;?>"><br><br>
<label for="pNum">Phone Number (optional)<br></label>
<input type="tel" id="pNum" name="pNum" value="<?php echo $pNum;?>"><br><br>
<label for="message">Message </label><span class="error"><?php echo $errMessage; ?></span><br>
<textarea id="message" name="message" style="height:100px"><?php echo $message;?></textarea>
<div class="g-recaptcha" data-sitekey="6LcR-DQUAAAAAB2cXajVPi7lpFEZ_z8vZrS97ZXo"></div><br>
<input type="submit" value="Submit">
</form>
<footer class="secondary_header footer">
<div class="copyright">© Car Audio Inc 2020, All Rights Reserved.</div>
</footer>
</div>
</body>
</html>
您可以在 if()
条件中将 isset()
函数设置为
if (isset($_POST)){...}
这意味着如果某些东西被 posted 或如果 $_POST
os 设置为某个值,代码将向前移动。
但是正如你之前提到的那样,它只是检查表单是否被 posted 并且每次表单被 posted 时根本没有任何值和代码正在进一步发展。
终于想通了
为未来像我这样的小白们贴出答案,希望对大家有帮助:
<?php
$to = 'example@caraudioinc.com';
$subject = 'Car Audio Inc Form Message';
$errName = $errEmail = $errMessage = $errCaptcha = "";
$name = $email = $pNum = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$pNum = test_input($_POST["pNum"]);
$message = test_input($_POST["message"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errName = "*Name field is required.";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Invaild name.";
}
}
if (empty($_POST["email"])) {
$errEmail = "*Email field is required.";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid E-mail.";
}
}
if (empty($_POST["message"])) {
$errMessage = "*Message field is required.";
} else {
$message = test_input($_POST["message"]);
}
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'insert-your-private-key-here';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
} else {
$errCaptcha = '*reCAPTCHA failed.';
}
$body = <<<EMAIL
Message from: $name
$message
Phone: $pNum
Email: $email
EMAIL;
$header = "From: $email";
if (!$errName && !$errEmail && !$errMessage && $responseData->success) {
if($_POST) {
mail($to, $subject, $body, $header);
header('Location: /sent');
}
}
}
?>
我讨厌寻求帮助.. 但我不知道花了多少时间来弄清楚如何使用 recaptcha 为这个表单实施验证步骤。
我知道我需要在某处定义私钥,但大多数其他指南都在使用 "isset" 函数,我只是不知道在 php 的哪个部分执行此操作。
目前,该按钮显示出来并且可以选中。但是当我点击提交时,无论该框是否被选中,它都会发送。 (只是解释以防万一。)
谢谢,抱歉。
<?php
$to = 'example@caraudioinc.com';
$subject = 'CarAudioInc.com Form Message';
$errName = $errEmail = $errMessage = "";
$name = $email = $pNum = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$pNum = test_input($_POST["pNum"]);
$message = test_input($_POST["message"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errName = "*Name field is required.";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Invaild name.";
}
}
if (empty($_POST["email"])) {
$errEmail = "*Email field is required.";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid E-mail.";
}
}
if (empty($_POST["message"])) {
$errMessage = "*Message field is required.";
} else {
$message = test_input($_POST["message"]);
}
$body = <<<EMAIL
Message from: $name
$message
Phone: $pNum
Email: $email
EMAIL;
$header = "From: $email";
if (!$errName && !$errEmail && !$errMessage ) {
if($_POST) {
mail($to, $subject, $body, $header);
header('Location: /sent');
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=1152">
<script src="https://www.google.com/recaptcha/api.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-100617570-1" ></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-100617570-1');
</script>
<!--Google meta tags -->
<title>Car Audio Inc | Contact</title>
<!--fb meta tags -->
<link rel="canonical" href="https://www.caraudioinc.com/contact" />
<link href="css/multiColumnTemplate.css" hreflang="en" rel="stylesheet" type="text/css">
<link rel="icon" href="images/favicon.png" type="image/png">
</head>
<body>
<div class="container">
<header>
<div class="primary_header">
<nav class="secondary_header" id="menu">
<ul>
<li><a href="/">HOME</a></li>
<li><a href="about">ABOUT</a></li>
<li><a href="services">SERVICES</a></li>
<li class="current"><a href="contact">CONTACT</a></li>
</ul>
</nav>
</header>
<div class="contactForm">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"><br>
<label for="name">Name </label><span class="error"><?php echo $errName; ?></span><br>
<input type="text" id="name" name="name" value="<?php echo $name;?>"><br><br>
<label for="email">Email </label><span class="error"><?php echo $errEmail; ?></span><br>
<input type="email" id="email" name="email" value="<?php echo $email;?>"><br><br>
<label for="pNum">Phone Number (optional)<br></label>
<input type="tel" id="pNum" name="pNum" value="<?php echo $pNum;?>"><br><br>
<label for="message">Message </label><span class="error"><?php echo $errMessage; ?></span><br>
<textarea id="message" name="message" style="height:100px"><?php echo $message;?></textarea>
<div class="g-recaptcha" data-sitekey="6LcR-DQUAAAAAB2cXajVPi7lpFEZ_z8vZrS97ZXo"></div><br>
<input type="submit" value="Submit">
</form>
<footer class="secondary_header footer">
<div class="copyright">© Car Audio Inc 2020, All Rights Reserved.</div>
</footer>
</div>
</body>
</html>
您可以在 if()
条件中将 isset()
函数设置为
if (isset($_POST)){...}
这意味着如果某些东西被 posted 或如果 $_POST
os 设置为某个值,代码将向前移动。
但是正如你之前提到的那样,它只是检查表单是否被 posted 并且每次表单被 posted 时根本没有任何值和代码正在进一步发展。
终于想通了
为未来像我这样的小白们贴出答案,希望对大家有帮助:
<?php
$to = 'example@caraudioinc.com';
$subject = 'Car Audio Inc Form Message';
$errName = $errEmail = $errMessage = $errCaptcha = "";
$name = $email = $pNum = $message = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$pNum = test_input($_POST["pNum"]);
$message = test_input($_POST["message"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$errName = "*Name field is required.";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Invaild name.";
}
}
if (empty($_POST["email"])) {
$errEmail = "*Email field is required.";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid E-mail.";
}
}
if (empty($_POST["message"])) {
$errMessage = "*Message field is required.";
} else {
$message = test_input($_POST["message"]);
}
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'insert-your-private-key-here';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
} else {
$errCaptcha = '*reCAPTCHA failed.';
}
$body = <<<EMAIL
Message from: $name
$message
Phone: $pNum
Email: $email
EMAIL;
$header = "From: $email";
if (!$errName && !$errEmail && !$errMessage && $responseData->success) {
if($_POST) {
mail($to, $subject, $body, $header);
header('Location: /sent');
}
}
}
?>