簡體   English   中英

在joomla K2中使用php插件安裝reCaptcha

[英]Install reCaptcha using php plugin in joomla K2

我試圖在K2前端子目錄形式上實現reCaptcha。 我下載了PHP庫,安裝它並可以成功地以子目錄形式顯示capcha圖像,但最后一步讓我感到困惑。 我需要把代碼放到動作php文件中:

<?php
  require_once(JPATH_SITE.'/libraries/recaptcha/recaptchalib.php');
  $privatekey = "MY 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 {
    alert(0);
  }
  ?>

問題是由於某種原因,動作文件恰好是index.php 如果我只是把那段代碼放在那里,主頁停止加載警告我misspeld Captcha鍵。 我將在下面發布我的K2frontend php文件,希望你能幫助我,因為它對我來說是至關重要的功能。

<?php
defined('_JEXEC') or die('Restricted access');

$document = & JFactory::getDocument();
$document->addScriptDeclaration("
    Joomla.submitbutton = function(pressbutton){
        if (pressbutton == 'cancel') {
            submitform( pressbutton );
            return;
        }
        if (\$K2.trim(\$K2('#title').val()) == '') {
            alert( '".JText::_('K2_ITEM_MUST_HAVE_A_TITLE', true)."' );
        }
        else if (\$K2.trim(\$K2('#catid').val()) == '0') {
            alert( '".JText::_('K2_PLEASE_SELECT_A_CATEGORY', true)."' );
        }
        else {
            syncExtraFieldsEditor();
            \$K2('#selectedTags option').attr('selected', 'selected');
            submitform( pressbutton );
        }
    }
");

?>
<div id="overallholderarticle" style="overflow:hidden;">

<form action="index.php" enctype="multipart/form-data" method="post" name="adminForm" id="adminForm">
    <?php if($this->mainframe->isSite()): ?>
    <div id="k2FrontendContainer">
        <div id="k2Frontend" style="background: url('<?php echo JURI::base(); ?>templates/<?php echo $template; ?>/images/bg_windows_bright_noise.png');">
            <div id="k2FrontendEditToolbar">
                <div class="titenewseditor">
                    <?php echo (JRequest::getInt('cid')) ? JText::_('K2_EDIT_ITEM') : JText::_('Добавить новость'); ?>
                </div>
            </div>
            <div class="clr"></div>
            <?php if(!$this->permissions->get('publish')): ?>
            <div id="k2FrontendPermissionsNotice">
                <p><?php echo JText::_('K2_FRONTEND_PERMISSIONS_NOTICE'); ?></p>
            </div>
            <?php endif; ?>
            <?php endif; ?>
            <div id="k2ToggleSidebarContainer"> <a href="#" id="k2ToggleSidebar"><?php echo JText::_('K2_TOGGLE_SIDEBAR'); ?></a> </div>
            <table cellspacing="0" cellpadding="0" border="0" class="adminFormK2Container">
                <tbody>
                    <tr>
                        <td>
                            <table class="adminFormK2">
                                <tr>
                                    <td class="adminK2LeftCol">
                                        <label for="title"><?php echo JText::_('K2_TITLE'); ?></label>
..........................
.........CODE HERE........
..........................

            </table>
            <div class="clr"></div>
            <?php if($this->mainframe->isSite()): ?>
        </div>
    </div>
    <!--BOTTOM ACTION BUTTONS-->
    <div id="k2Frontend">
        <!--reCAPCHA-->
        <?php
        require_once(JPATH_SITE.'/libraries/recaptcha/recaptchalib.php');
        $publickey = "MY PUBLIC KEY";
        echo recaptcha_get_html($publickey);
        ?>
            <table class="k2FrontendToolbar" cellpadding="2" cellspacing="4">
                <tr>
                    <td id="toolbar-save" class="button">
                        <a class="toolbar" href="#" onclick="javascript: submitbutton('save'); return false;"> <span title="<?php echo JText::_('K2_SAVE'); ?>" class="icon-32-save"></span> <?php echo JText::_('K2_SAVE'); ?> </a>
                    </td>
                    <td id="toolbar-cancel" class="button"> 
<a class="toolbar" href="#" onclick="javascript:history.go(-1)"> <span title="<?php echo JText::_('K2_CANCEL'); ?>" class="icon-32-cancel"></span> <?php echo JText::_('K2_CLOSE'); ?> </a> 
</td> 
                </tr>
            </table>
    </div>
    <?php endif; ?>
</form>
</div>

如果您在管理部分工作,您可以在項目控制器的保存功能中驗證驗證碼。 在此文件administrator/components/com_k2/controllers/item.php檢查以下函數並添加代碼。

function save() {
 JRequest::checkToken() or jexit('Invalid Token');
 //add library and captcha validation code here
 $model = & $this->getModel('item');
 $model->save();
}

最好檢查或嘗試插件。

希望這也會有所幫助 -

1) 如何使用joomla recaptcha插件到我的自定義模塊

2) 在Joomla中使用ReCaptcha和我的自定義表單

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM