reCAPTCHA在同一索引中的图像和验证

时间:2014-08-10 01:18:56

标签: javascript php forms alert recaptcha

我试图让reCaptcha在同一个索引中工作。我试过这个:

<html>
  <body>
    <form action="index.php" method="post">
      <?php
        require_once('recaptchalib.php');
        $privatekey = "YOUR_PRIVATE_KEY_HERE";
        if (isset($_POST['submit'])) {
          $resp = recaptcha_check_answer ($privatekey,
            $_SERVER["REMOTE_ADDR"],
            $_POST["recaptcha_challenge_field"],
            $_POST["recaptcha_response_field"]);

          if (!$resp->is_valid) {
            echo "<script language='javascript'>
              window.alert('Sorry cannot send email as you have failed to provide correct captcha! Try again...')
            </script>";
          } else {
            echo "<script language='javascript'>
              window.alert('Thank you for submitting the track. We will be in touch with you very soon. Click Ok to redirect you to the Homepage.')
            window.location.href='http://www.xmasterofficial.tk/'
           </script>";
          }
       }
       $publickey = "YOUR_PUBLIC_KEY_HERE";
       echo recaptcha_get_html($publickey);
      ?>
     <br/>
     <input type="submit" value="submit" />
    </form>
  </body>
</html>

问题

即使答案正确或错误,window.alertwindow.location.href也无效。

当你点击提交按钮时,它应该弹出一个window.alert如果答案是好的并重定向到主页或弹出一个window.alert来重试,如果答案是错误的。

编辑:您可以运行它。你需要google中的recaptchalib.php(链接:https://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest

1 个答案:

答案 0 :(得分:0)

找到解决方案:

<div class="php">
<?php
require_once('recaptchalib.php');
$privatekey = "YOUR_PRIVATE_KEY_HERE";
if(isset($_POST['email'])) {
$resp = null;
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
echo "<script type='text/javascript'>
window.alert('Sorry cannot send email as you have failed to provide correct captcha! Try again...')
window.location.href='http://www.xmasterofficial.tk/submit'
</script>";
} else {
if(isset($_POST['email'])) {
$email_to = "sumutiumarius@gmail.com";
$email_subject = "Xmaster - Track Submission";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['artistname']) ||
!isset($_POST['email']) ||
!isset($_POST['track']) ||
!isset($_POST['links']) ||
!isset($_POST['type'])  ||
!isset($_POST['download'])  ||
!isset($_POST['free'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$artistname = $_POST['artistname'];
$emaila = $_POST['email'];
$track = $_POST['track'];
$links = $_POST['links'];
$type = $_POST['type'];
$download = $_POST['download'];
$free = $_POST['free'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$emaila)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($artistname) < 2) {
$error_message .= 'The Artist Name you entered does not appear to be valid.<br />';
}
if(strlen($track) < 2) {
$error_message .= 'The Track Name you entered does not appear to be valid.<br />';
}
if(strlen($links) < 2) {
$error_message .= 'The Links you entered does not appear to be valid.<br />';
}
if(strlen($type) < 2) {
$error_message .= 'The Type you entered do not appear to be valid.<br />';
}
if(strlen($download) < 2) {
$error_message .= 'The Download Link you entered do not appear to be valid.<br />';
}
if(strlen($free) < 1) {
$error_message .= 'The answer you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Submission form details below:\n\n";
    function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Your Name: \n".clean_string($name)."\n\n";
$email_message .= "Artist Name: \n".clean_string($artistname)."\n\n";
$email_message .= "Your Email: \n".clean_string($emaila)."\n\n";
$email_message .= "Track Name: \n".clean_string($track)."\n\n";
$email_message .= "Your Links: \n".clean_string($links)."\n\n";
$email_message .= "Track Type: \n".clean_string($type)."\n\n";
$email_message .= "Download: \n".clean_string($download)."\n\n";
$email_message .= "Download Free: \n".clean_string($free)."\n\n";
$headers = 'From: '.$emaila."\r\n".
'Reply-To: '.$emaila."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo "<script type='text/javascript'>
window.alert('Thank you for submitting the track. We will be in touch with you very soon. Click Ok to redirect you to the Homepage.')
window.location.href='http://www.xmasterofficial.tk/'
</script>";
}
}
}
?>
</div>
<form action="index.php" method="POST">
<table id="tabel" cellspacing="0" cellpadding="0" border="0">
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/name.png">
</td>
<td valign="top">
<input type="text" name="name" class="rounded" maxlength="25">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/artist.png">
</td>
<td valign="top">
<input type="text" name="artistname" class="rounded" maxlength="25">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/email.png">
</td>
<td valign="top">
<input type="text" name="email" class="rounded" maxlength="40">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/track.png">
</td>
<td valign="top">
<input type="text" name="track" class="rounded" maxlength="60">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/links.png">
</td>
<td valign="top">
<textarea name="links" class="rounded" maxlength="500" cols="28" rows="2"></textarea>
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/type.png">
</td>
<td valign="top">
<input type="text" name="type" class="rounded" maxlength="20">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/downloadlink.png">
</td>
<td valign="top">
<input type="text" name="download" class="rounded" maxlength="150">
</td>
</tr>
<tr>
<td valign="top">
<img src="http://www.xmasterofficial.tk/Images/downloadfree.png">
</td>
<td valign="top">
<input type="text" name="free" class="rounded" maxlength="3">
</td>
</tr>
</table>
<input id="left" type="submit" class="submit" value=" ">
<input id="right" type="reset" value=" ">
<div id="captcha">
<?php
require_once('recaptchalib.php');
$publickey = "YOUR_PUBLIC_KEY_HERE";
echo recaptcha_get_html($publickey, $error);
?>
</div>
</form>

P.S。这是一个带有工作验证码的完整表格。您可以根据需要使用它。不要忘记用自己的密钥替换$privatekey$publickey。您可以从此处获取密钥:Google reCaptcha