Я пытался реализовать Google Reaptcha на моем сайте. но по некоторым причинам это не работает. Форма не отправляется. также ошибка проверки контрольной суммы не работает. я вставил свой секретный ключ Google Reaptcha, а также.
Я нашел код подтверждения капчи из интернета
Что я делаю не так?
<?php
if ( isset( $_POST[ 'g-recaptcha-response' ] ) && $_POST[ 'g-recaptcha-response' ] ) {
var_dump( $_POST );
$secret = "my secret key";
$ip = $_SERVER[ 'REMOTE_ADDR' ];
$captcha = $_POST[ 'g-recaptcha-response' ];
$rsp = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip" );
var_dump( $rsp );
$arr = json_decode( $rsp, TRUE );
if ( $arr[ 'success' ] ) {
$address = "[email protected]";
$CompanyName = $_POST[ 'CompanyName' ];
$ContactName = $_POST[ 'ContactName' ];
$City = $_POST[ 'City' ];
$State = $_POST[ 'State' ];
$Phone = $_POST[ 'Phone' ];
$Email = $_POST[ 'Email' ];
$FromFind = $_POST[ 'FromFind' ];
$Comments = $_POST[ 'Comments' ];
$Page = $_POST[ 'Page' ];
$headers = "From: $Email";
$headers .= "Reply-To: $Email";
$headers .= "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=utf-8";
$headers .= "Content-Transfer-Encoding: quoted-printable";
$e_subject = 'MicroShred.com Form Contact by ' . $ContactName . '.';
// Email message:
$msg = "You have been contacted by $ContactName, in $City $State, through your $Page. Their message is as follows:";
$msg .= "$Comments";
$msg .= "You can contact $ContactName via email, $Email or via phone $Phone.";
$msg .= "-------------------------------------------------------------------------------------------";
$msg .= "This message was sent to you via your website Contact Form";
$msg = wordwrap( $msg, 70 );
if ( mail( $address, $e_subject, $msg, $headers ) ) {
echo "<fieldset>";
echo "<div class=\"notification success closeable\">";
echo "<p><strong>Email Sent Successfully.</strong><br />";
echo "Thank you $name, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
} else {
echo ' please make sure that you have checked the captcha';
}
}
?>
Вот форма
<form method="post" action="/_contact-form/contact2.php" name="contactform" id="contactform" onSubmit="return validate_form();">
<input type="hidden" name="mode" value="Send" />
<input type="hidden" name="Page" value="Scheduled Shredding Services Page" />
<input type="hidden" name="path" value="/shredding-services-florida/scheduled-visits.php" />
<table width="60%" border="0" align="right" cellpadding="3" cellspacing="3" class="text">
<tr>
<td colspan="3" class="main"></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>Company Name</strong></div></td>
<td colspan="2"><input type="text" id="CompanyName" name="CompanyName" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>Contact Name</strong>*</div></td>
<td colspan="2"><input type="text" name="ContactName" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>City</strong></div></td>
<td colspan="2"><input type="text" name="City" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>State</strong></div></td>
<td colspan="2"><input type="text" name="State" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>Zip</strong>*</div></td>
<td colspan="2"><input type="text" name="Zip" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>Phone</strong>*</div></td>
<td colspan="2"><input type="text" name="Phone" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center"><strong>Email</strong>*</div></td>
<td colspan="2"><input type="text" name="Email" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td class="main"><div align="center">How did you find us? </div></td>
<td colspan="2"><input type="text" name="FromFind" size="30" /></td>
</tr>
<tr align="center" valign="middle">
<td colspan="3" class="main"><div align="center"><strong>Comments or Questions?</strong></div></td>
</tr>
<tr align="center" valign="middle">
<td colspan="3"><p align="center">
<textarea name="Comments" cols="55" rows="7" id="Comments"></textarea>
<div class="g-recaptcha" data-sitekey="here is the site key"></div>
</p></td>
</tr>
<tr align="center" valign="middle">
<td colspan="2"><p align="left"><strong>*</strong>= Required field </p></td>
<td><input name="submit" type="submit" value="Submit Form" /></td>
</tr>
<tr align="center" valign="middle">
<td colspan="3" class="main"> </td>
</tr>
</table>
</form>
Возможно, ваш веб-сервер блокирует вашу команду file_get_contents. Это можно проверить в деталях phpinfo, проверив, отключена ли директива allow_url_fopen. Если это так, file_get_contents не будет работать.
Столкнувшись с этой проблемой в прошлом, мне пришлось использовать curl, чтобы заставить процесс проверки работать, в качестве замены file_get_contents.
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'secret' => $secret,
'response' => $captcha,
'remoteip' => $ip
],
CURLOPT_RETURNTRANSFER => true
]);
$output = curl_exec($ch);
curl_close($ch);
$arr = json_decode($output);
if ( $arr->success !== true ) {
//Submission errored out
}
else {
//Send Email
}
Других решений пока нет …