[英]How to get the “really simple captcha” to work in a simple contact form?
我試圖根據本教程創建一個簡單的聯系表單
為了避免垃圾郵件攻擊,我嘗試將該聯系表單與非常簡單的captcha插件結合起來。
使用以下代碼,我可以在我的任何頁面中使用短代碼[contact]
,並且表單顯示但是在檢查驗證碼時出現問題。
這是插件的代碼:
function wptuts_get_the_ip() {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else {
return $_SERVER["REMOTE_ADDR"];
}
}
// short code function
function wptuts_contact_form_sc( $atts ) {
add_shortcode( 'contact', 'contact_form_shortcode' );
extract(shortcode_atts(array(
"email" => get_bloginfo('admin_email'),
"subject" => '',
"label_name" => 'Your Name',
"label_email" => 'Your E-mail Address',
"label_subject" => 'Your Answer',
"label_captcha" => 'Captcha',
"label_submit" => 'Submit',
"error_empty" => 'Please fill in all the required fields.',
"error_noemail" => 'Please enter a valid e-mail address.',
"success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
), $atts));
$captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin
$word = $captcha_instance->generate_random_word();
$prefix = mt_rand();
$image= $captcha_instance->generate_image( $prefix, $word );
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = false;
$required_fields = array("your_name", "email", "captcha", "subject");
foreach ($_POST as $field => $value) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$form_data[$field] = strip_tags($value);
}
foreach ($required_fields as $required_field) {
$value = trim($form_data[$required_field]);
if(empty($value)) {
$error = true;
$result = $error_empty;
}
}
if(!is_email($form_data['email'])) {
$error = true;
$result = $error_noemail;
}
if ($error == false) {
$captcha_text=$form_data['captcha'];
$correct = $captcha_instance->check( $prefix, $captcha_text );
if($correct==TRUE){
$email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
$email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip();
$headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
wp_mail($email, $email_subject, $email_message, $headers);
$result = $success;
$sent = true;
}
}
}
if($result != "") {
$info = '<div class="info">'.$result.'</div>';
}
$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
<div>
<label for="cf_name">'.$label_name.':</label>
<input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
</div>
<div>
<label for="cf_email">'.$label_email.':</label>
<input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
</div>
<div>
<label for="cf_subject">'.$label_subject.':</label>
<input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
</div>
<div>
<label for="cf_message">'.$label_captcha.':</label>
<input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/>
</div>
<div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div>
<div>
<input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
</div>
</form>';
if($sent == true) {
return $info;
} else {
return $info.$email_form;
}
}
add_shortcode('contact', 'wptuts_contact_form_sc');
// add plugins style to frontend
function prize_game_contact_form() {
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__));
wp_enqueue_style('prize_game_contact_form');
}
add_action('wp_enqueue_scripts', 'prize_game_contact_form');
我沒有添加$captcha_instance->remove( $prefix );
到目前為止,因為它不能正常工作。 似乎每次我點擊提交按鈕時都會創建一個新單詞並且函數$captcha_instance->check()
檢查新單詞是否等於輸入字段中的驗證碼文本,但這些單詞當然不匹配,因為它是已經是一個新詞了。 我不明白為什么單擊提交按鈕時會創建一個新單詞。 錯誤在哪里?
你在使用Wordpress嗎? 不要試圖重新發明輪子。
下載一個免費的插件,如Contact Form 7 ,其中包括一個支持Ajax(不需要頁面刷新)的Askimet Spam Filter,以及CAPTCHA系統。
我給你的建議是這樣的:
我相信問題是你每次運行函數時都會生成一個新的$prefix
。 因此無法運行check()
或remove()
因為表單上使用的$prefix
是一個新值。
在表單中,創建一個隱藏的輸入字段,其中包含正在使用的$prefix
的值。 因此該值與表單一起發布。 當函數在表單發布后第二次運行時,使用posted $prefix
值來執行check()
和remove()
。 應該這樣做。
這是一篇關於如何將Really Simple Captcha插件與我剛才提到的隱藏前綴字段集成到您的插件或主題中的詳細文章: http : //www.lost-in-code.com/platforms/wordpress/wordpress-plugins/使用WordPress - -真的-簡單的驗證碼/
如果您需要任何幫助,請隨時告訴我。
如果你沒有使用WordPress,那么你可以查看這個簡單的表格和驗證碼的想法 - 如果你可以抓住驗證碼部分,驗證碼變得非常復雜和過度設計,當它所需要做的只是區分人和非人類用於大多數目的。
只有在檢查用戶答案的正確性后,才必須創建變量$ word,$ prefix和$ image,並且必須通過隱藏的輸入字段傳遞$ prefix值。 我沒有檢查過,但是這段代碼應該可以正常工作:
function wptuts_get_the_ip() {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else {
return $_SERVER["REMOTE_ADDR"];
}
}
// short code function
function wptuts_contact_form_sc( $atts ) {
add_shortcode( 'contact', 'contact_form_shortcode' );
extract(shortcode_atts(array(
"email" => get_bloginfo('admin_email'),
"subject" => '',
"label_name" => 'Your Name',
"label_email" => 'Your E-mail Address',
"label_subject" => 'Your Answer',
"label_captcha" => 'Captcha',
"label_submit" => 'Submit',
"error_empty" => 'Please fill in all the required fields.',
"error_noemail" => 'Please enter a valid e-mail address.',
"success" => 'Thanks for your e-mail! We\'ll get back to you as soon as we can.'
), $atts));
$captcha_instance = new ReallySimpleCaptcha(); //imports really simple captcha plugin
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = false;
$required_fields = array("your_name", "email", "captcha", "subject");
foreach ($_POST as $field => $value) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$form_data[$field] = strip_tags($value);
}
foreach ($required_fields as $required_field) {
$value = trim($form_data[$required_field]);
if(empty($value)) {
$error = true;
$result = $error_empty;
}
}
if(!is_email($form_data['email'])) {
$error = true;
$result = $error_noemail;
}
if ($error == false) {
$captcha_text=$form_data['captcha'];
$captcha_prefix=$form_data['prefix'];
$correct = $captcha_instance->check( $captcha_prefix, $captcha_text );
if($correct==TRUE){
$email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
$email_message = $form_data['captcha'] . "\n\nIP: " . wptuts_get_the_ip();
$headers = "From: ".$form_data['your_name']." <".$form_data['email'].">\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
wp_mail($email, $email_subject, $email_message, $headers);
$result = $success;
$sent = true;
}
}
}
$word = $captcha_instance->generate_random_word();
$prefix = mt_rand();
$image= $captcha_instance->generate_image( $prefix, $word );
if($result != "") {
$info = '<div class="info">'.$result.'</div>';
}
$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
<div>
<label for="cf_name">'.$label_name.':</label>
<input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
</div>
<div>
<label for="cf_email">'.$label_email.':</label>
<input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
</div>
<div>
<label for="cf_subject">'.$label_subject.':</label>
<input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
</div>
<div>
<label for="cf_message">'.$label_captcha.':</label>
<input type="text" name="captcha" id="cf_message" size="50" maxlength="50" value=""/>
<input type="hidden" name="prefix" id="prefix" value="'.$prefix.'"/>
</div>
<div id="captcha"><span id="captcha_text">Captcha:</span><img id="captcha_image" src="'.plugins_url("really-simple-captcha/tmp/" . $image) . '" alt="" /></div>
<div>
<input type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
</div>
</form>';
if($sent == true) {
return $info;
} else {
return $info.$email_form;
}
}
add_shortcode('contact', 'wptuts_contact_form_sc');
// add plugins style to frontend
function prize_game_contact_form() {
wp_register_style('prize_game_contact_form', plugins_url('css/style.css',__FILE__));
wp_enqueue_style('prize_game_contact_form');
}
add_action('wp_enqueue_scripts', 'prize_game_contact_form');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.