Recaptcha 到自定义 PHP 表单
Recaptcha into custom PHP form
我正在尝试将验证码添加到我的自定义 PHP 表单中,但我对如何操作感到困惑。 Recaptcha 需要将 recaptchalib.php
添加到表单中,但如果我添加 verify.php
那么我的表单将不会处理,因为我正在使用我的 PHP 文件来处理我的表单。
<form method="POST" action="process.php" id="form-ok">
documentation 有点混乱。我的问题是,我需要做什么来处理两个动作?
知道如何让它工作吗?
Process.php
<?php
$redirectTo = '/thankyou.html';
$subject = 'New message from site'; // Email SUBJECT field
$receive = array(
'example@example.com'
);
if($_POST['email_check'] == '') {
if (isset($_POST['first_name'])){
$message = '<table width="100%" border="0" cellspacing="0" cellpadding="8" style="border:1px solid #f3f3f3">
<tr>
<td colspan="3" height="30" style="font-size:20px"><strong>' . $subject . '</strong></td>
</tr>
<tr>
<td width="100" bgcolor="#f3f3f3"><strong>First Name: </strong></td>
<td width="14" height="30" bgcolor="#f3f3f3"> </td>
<td width="305" bgcolor="#f3f3f3">' . $_POST ['first_name'] . '</td>
</tr>
<tr>
<td><strong>Last Name: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['last_name'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Email: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">' . $_POST ['email'] . '</td>
</tr>
<tr>
<td><strong>Phone Number: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['phone'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Check: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">';
foreach($_POST['role'] as $value)
{
$message.=$value.'<br>';
}
$message.='</td>
</tr>
<tr>
<td><strong>Message: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['message'] . '</td>
</tr>
<tr>
<td><strong>Referer:</strong></td>
<td width="14" height="30"> </td>
<td>' . $_SERVER ['HTTP_REFERER'] . '</td>
</tr>
<tr>
</table>';
for ($i = 0; $i < count($receive); $i++){
$to = $receive[$i];
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From: ' . $_POST['email'] . "\n" . 'Reply-To: ' . $_POST['email'] . "\n";
mail($to, $subject, $message,$headers);
}
header('Location: '.$redirectTo);
}
}
else{
header('Location:'.$_SERVER['HTTP_REFERER']); die();
}
?>
客户端(如何显示验证码图像)
<form method="post" action="process.php">
<?php
require_once('recaptchalib.php');
$publickey = "YOUR_PUBLIC_KEY"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form><br>
<!-- more of your HTML content -->
服务器端
以下代码应放在 process.php 文件的顶部:
<?php
require_once('recaptchalib.php');
$privatekey = "YOUR_PRIVATE_KEY";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
将您的 recaptchalib.php 添加到您的目录中。
你的PROCESS.PHP:
require_once "../recaptchalib.php"; // where you store recaptchalib.php
$secret = "6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"; //your secret key
$resp = null;
$error = null;
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
$resp = $reCaptcha->verifyResponse($_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]);
}
if (isset($_POST['cmdlogin'])){
if ($resp != null && $resp->success) {
echo "<script>alert('Success Verifying Recaptcha!');</script>";
echo "<meta http-equiv='refresh' content='0; url=login.php'>";
exit();
}
<form method="post" action="process.php">
.....other codes---
<div class="g-recaptcha" data-
sitekey="6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx">
</div>
.....other codes---
</form>
完整教程,点击这里:
https://github.com/google/ReCAPTCHA/tree/master/php
我正在尝试将验证码添加到我的自定义 PHP 表单中,但我对如何操作感到困惑。 Recaptcha 需要将 recaptchalib.php
添加到表单中,但如果我添加 verify.php
那么我的表单将不会处理,因为我正在使用我的 PHP 文件来处理我的表单。
<form method="POST" action="process.php" id="form-ok">
documentation 有点混乱。我的问题是,我需要做什么来处理两个动作?
知道如何让它工作吗?
Process.php
<?php
$redirectTo = '/thankyou.html';
$subject = 'New message from site'; // Email SUBJECT field
$receive = array(
'example@example.com'
);
if($_POST['email_check'] == '') {
if (isset($_POST['first_name'])){
$message = '<table width="100%" border="0" cellspacing="0" cellpadding="8" style="border:1px solid #f3f3f3">
<tr>
<td colspan="3" height="30" style="font-size:20px"><strong>' . $subject . '</strong></td>
</tr>
<tr>
<td width="100" bgcolor="#f3f3f3"><strong>First Name: </strong></td>
<td width="14" height="30" bgcolor="#f3f3f3"> </td>
<td width="305" bgcolor="#f3f3f3">' . $_POST ['first_name'] . '</td>
</tr>
<tr>
<td><strong>Last Name: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['last_name'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Email: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">' . $_POST ['email'] . '</td>
</tr>
<tr>
<td><strong>Phone Number: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['phone'] . '</td>
</tr>
<tr>
<td bgcolor="#f3f3f3"><strong>Check: </strong></td>
<td bgcolor="#f3f3f3" width="14" height="30"> </td>
<td bgcolor="#f3f3f3">';
foreach($_POST['role'] as $value)
{
$message.=$value.'<br>';
}
$message.='</td>
</tr>
<tr>
<td><strong>Message: </strong></td>
<td width="14" height="30"> </td>
<td>' . $_POST ['message'] . '</td>
</tr>
<tr>
<td><strong>Referer:</strong></td>
<td width="14" height="30"> </td>
<td>' . $_SERVER ['HTTP_REFERER'] . '</td>
</tr>
<tr>
</table>';
for ($i = 0; $i < count($receive); $i++){
$to = $receive[$i];
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= 'From: ' . $_POST['email'] . "\n" . 'Reply-To: ' . $_POST['email'] . "\n";
mail($to, $subject, $message,$headers);
}
header('Location: '.$redirectTo);
}
}
else{
header('Location:'.$_SERVER['HTTP_REFERER']); die();
}
?>
客户端(如何显示验证码图像)
<form method="post" action="process.php">
<?php
require_once('recaptchalib.php');
$publickey = "YOUR_PUBLIC_KEY"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form><br>
<!-- more of your HTML content -->
服务器端 以下代码应放在 process.php 文件的顶部:
<?php
require_once('recaptchalib.php');
$privatekey = "YOUR_PRIVATE_KEY";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
将您的 recaptchalib.php 添加到您的目录中。
你的PROCESS.PHP:
require_once "../recaptchalib.php"; // where you store recaptchalib.php
$secret = "6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx"; //your secret key
$resp = null;
$error = null;
$reCaptcha = new ReCaptcha($secret);
if ($_POST["g-recaptcha-response"]) {
$resp = $reCaptcha->verifyResponse($_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]);
}
if (isset($_POST['cmdlogin'])){
if ($resp != null && $resp->success) {
echo "<script>alert('Success Verifying Recaptcha!');</script>";
echo "<meta http-equiv='refresh' content='0; url=login.php'>";
exit();
}
<form method="post" action="process.php">
.....other codes---
<div class="g-recaptcha" data-
sitekey="6Le2g_sSxxxxxxxxxxxxxxxxxxxxxxxx">
</div>
.....other codes---
</form>
完整教程,点击这里: https://github.com/google/ReCAPTCHA/tree/master/php