XOOPSのお問い合わせモジュールliaiseでスパム除けする
画像認証改造っていうのがあるのは作った後で知った。
方針
所定の文字列を入れてもらい、それがPOST値になかったらエラー。(やり直し)
下記のサンプルではその文字列は123456とする。
modules/liaise/include/form_render.php
フォームに入力欄を入れる。
下記のコードを
<<?php $form_output->addElement(new XoopsFormButton('', 'submit', $form->getVar('form_submit_text'), 'submit'));
の上らへんに入れる。
<<?php /* spam avoid form element */ $nospam_tray = new XoopsFormElementTray("スパム防止"); $nospam_tray->addElement(new XoopsFormText('<p class="nospam">迷惑投稿の防止のため、下記に半角英数字で「123456」と入力してください。(Please input below text box "123456" in alphanumeric bytes as avoid spam message.)</p>', 'nospam', 8, 8)); $form_output->addElement($nospam_tray);
modules/liaise/index.php
入力値が123456であることをチェック。
<?php if (mb_convert_kana($_POST['nospam'], 'KVa', 'UTF-8') != '123456'){ redirect_header(LIAISE_URL."?form_id=".intval($_POST['form_id']), 5, "スパム防止の文字列を入力してください。Please input anti-spam strings."); exit(); }
これを最後らへんの
require 'include/form_execute.php';
の上に入れる。
modules/liaise/templates/liaise_form.html
入力に気が付かないとかわいそうなので、フォームにJavaScriptでアラートを出すようにする。
<script> $(function(){ $('form#<{$form_output.name}>').submit(function(e){ if ($('input[name="nospam"]').val() != "123456"){ alert("スパム対策の文字列を入力してください。\nPlease input spam avoid strings."); e.preventDefault(); } }); }); </script>
を一番上に追加。
こんな感じ。