AnQi CMS is an efficient content management system that provides a convenient content comment management function, aiming to promote user interaction and the construction of the content ecosystem.Through the built-in comment management tool, you can review, reply, like, and other operations on comments.As the website traffic increases, the pressure of manual review will also grow.Introduce the captcha, which can add a human verification barrier before the comment submission, effectively filtering out the vast majority of spam comments published by robots, thereby significantly improving the quality and security of the comment area.

Add captcha for the comment feature, which is mainly divided into two steps in AnQiCMS: first, enable the background settings, and then integrate the captcha element into the comment submission form on the website front end.

The second step, which is also the most critical one, is to add the captcha display element to the comment submission form on your website's frontend.The AnQiCMS template system is flexible, you can insert the following code at the appropriate position in the comment submission form according to the structure of your template file.archiveDetailThe template where the tag is located or a dedicated comment list page (such as)comment/list.htmlorcomment_list.html)

The core code snippet that needs to be added to the template file:

<div style="display: flex; clear: both">
  <input type="hidden" name="captcha_id" id="captcha_id">
  <input type="text" name="captcha" required placeholder="请填写验证码" class="layui-input" style="flex: 1">
  <img src="" id="get-captcha" style="width: 150px;height: 56px;cursor: pointer;" />
  <script>
    document.getElementById('get-captcha').addEventListener("click", function (e) {
      fetch('/api/captcha')
              .then(response => {
                return response.json()
              })
              .then(res => {
                document.getElementById('captcha_id').setAttribute("value", res.data.captcha_id)
                document.getElementById('get-captcha').setAttribute("src", res.data.captcha)
              }).catch(err =>{console.log(err)})
    });
    document.getElementById('get-captcha').click();
  </script>
</div>

If your website template integrates the jQuery library, you can also use a more concise jQuery syntax:

<div style="display: flex; clear: both">
  <input type="hidden" name="captcha_id" id="captcha_id">
  <input type="text" name="captcha" required placeholder="请填写验证码" class="layui-input" style="flex: 1">
  <img src="" id="get-captcha" style="width: 150px;height: 56px;cursor: pointer;" />
  <script>
    // jQuery 调用方式
    $('#get-captcha').on("click", function (e) {
      $.get('/api/captcha', function(res) {
        $('#captcha_id').attr("value", res.data.captcha_id)
        $('#get-captcha').attr("src", res.data.captcha)
      }, 'json')
    })
    $('#get-captcha').click(); // 页面加载时自动触发一次,显示验证码
  </script>
</div>

The core of this code is:

  • A hiddeninputfieldcaptcha_idIt is used to store the unique identifier of the current captcha, which is sent along with the form when submitting comments for backend verification.
  • A textinputfieldcaptchaProvide the characters displayed in the image for user input.
  • Oneimgtag, itssrcThe attribute will dynamically load captcha images via JavaScript.
  • The accompanying JavaScript code will be executed when the page loads (viadocument.getElementById('get-captcha').click();or$('#get-captcha').click();) automatically to/api/captchamake a request to the interface, retrieve and display the captcha image and the correspondingcaptcha_id. It also listens for click events on the captcha image, so that the user can refresh and get a new captcha by clicking on the image if they cannot see it clearly.

After integrating the template code, your comment form will require users to enter a captcha when submitted.This simple barrier can effectively prevent the intrusion of most malicious programs, keeping your comment section clear.After integration, we recommend that you perform several comment submission tests on the website to verify that the captcha is displayed normally, whether it can be refreshed correctly, and whether it can pass verification after submission to ensure that the function runs normally.Although the captcha is not infallible, it can greatly increase the attack threshold of automated programs, providing a solid guarantee for the content security of your website.


Frequently Asked Questions (FAQ)

1. I have enabled the comment captcha function on the AnQiCMS backend, but the captcha is still not displayed on the front-end comment form, why is that?

This is usually because you have only enabled the feature in the background, but did not add the code to display the captcha on your website template file.The background is enabled just to let the system perform captcha verification logic when receiving comments, the captcha image and input box on the front end need you to manually integrate the above code snippet into your comment form HTML template and ensure that JavaScript can execute correctly to load the captcha image.

2. How should I troubleshoot if the captcha image does not display or there is no reaction when refreshing?

First, please check your browser's developer tools (usually opened by pressing F12) console and network tabs.

  • Network (Network) tab:Check if there is a focus on/api/captchaMake a request and check the response status code (it should be 200). If the request fails or an error is returned, it may be due to server configuration issues or incorrect API path.
  • Console (Console) tab:Check for any JavaScript error messages. Common errors may include element ID not found (getElementByIdElement not found, URL incorrect due to network errors, etc.
  • Make sure your JavaScript code is placed correctly and not interfered with by other scripts. Additionally, check the HTML inimglabel'sid="get-captcha"andinput type="hidden"ofid="captcha_id"whether it is correct.

3. Does the comment captcha affect user experience? Is it possible to overcomplicate it?

Any form of captcha can affect user experience, especially when entering on mobile devices.However, to ensure the quality and security of the comment area, captcha is one of the most direct and effective means at present.The captcha design of AnQiCMS is usually lightweight (such as simple numeric-letter combinations), aimed at minimizing interference with users.If your website's spam comment problem is not serious, or you want to further optimize the user experience, you can consider focusing on more intelligent human verification solutions in the future versions of AnQiCMS, such as behavior verification codes that do not require user input, and so on.Currently, reasonable use of captcha is an effective balance point for maintaining the good order of the comment section.