How to enable and use the captcha feature of the留言表单in AnQiCMS?

Calendar 👁️ 71

The website message board is a good place for interaction with visitors, bringing the website closer to the users.However, without proper protection, the message board will soon be flooded with various spam messages, malicious submissions, and even automated scripts, which not only affects the cleanliness of the website but may also consume server resources and even damage the reputation of the website.Luckyly, AnQiCMS provides a simple and effective solution: captcha function.

In AnQiCMS, enabling and using captcha for the message form can significantly improve the security of the website and effectively prevent most spam information from bothering.The entire process is divided into two major parts: backend activation and frontend template integration. Next, we will introduce how to implement it step by step.

The first step: Enable captcha function in AnQiCMS backend

Firstly, we need to enable the captcha function in the AnQiCMS backend management interface.This is a global setting that, once enabled, the system will provide captcha support in the background for the message form.

  1. Log in to your AnQiCMS backend.
  2. Find and click in the left navigation bar“Function Management”Option.
  3. Select in the expanded feature list“Website Message Management”.
  4. After entering the website message management page, you will see a name called“Turn on the message comment captcha function”.
  5. Check this option and then click the bottom of the page“Save”button.

After completing this step, the AnQiCMS backend has completed the preparation of the captcha function.Please note that enabling it in the background will not make the captcha appear on the website front end immediately, we still need to modify the front-end template.

Step two: Integrate captcha in the comment form (front-end template)

In order for the captcha to truly display and be put into use, we still need to add the corresponding HTML and JavaScript code to the website's front-end comment form template.

Generally, the AnQiCMS website's message form template file is locatedtemplateUnder the directory/guestbook/index.htmlor in a file with a similar name. You need to find and edit the template file you are using through FTP, SSH, or Baota panel file management features.

In the comment form's<form>Inside the tag, you need to find a suitable position to insert the captcha input box and image.Generally, we would place it below the message content input box and above the submit button to ensure that users can clearly see and enter the captcha before submitting.

The following are the HTML and JavaScript code snippets that need to be added to the template:

<div style="display: flex; clear: both; margin-bottom: 15px;">
  <input type="hidden" name="captcha_id" id="captcha_id">
  <input type="text" name="captcha" required placeholder="请填写验证码" class="layui-input" style="flex: 1; margin-right: 10px;">
  <img src="" id="get-captcha" style="width: 150px; height: 56px; cursor: pointer; border: 1px solid #e6e6e6;" alt="验证码" title="点击刷新验证码" />
  <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>

Code explanation:

  • input type="hidden" name="captcha_id"This is a hidden field used to store the unique identifier of the current captcha. The system uses this ID to match whether the captcha entered by the user is correct.
  • input type="text" name="captcha": This is the text box for entering the user's captcha.
  • img src="" id="get-captcha": This is the area for displaying the captcha image.
  • scriptJavaScript code within the label:
    • It is for the captcha image (id="get-captcha"Added a click event listener. A network request is triggered when the user clicks on the image.
    • fetch('/api/captcha')This will be sent to the AnQiCMS backend./api/captchaThe interface sends a request to get a new captcha image and its correspondingcaptcha_id.
    • document.getElementById('captcha_id').setAttribute("value", res.data.captcha_id): The obtainedcaptcha_idupdate it to the hidden field.
    • document.getElementById('get-captcha').setAttribute("src", res.data.captcha): Update the obtained captcha image path to<img>label'ssrcThe attribute is used to display a new captcha image.
    • document.getElementById('get-captcha').click()This line of code is used to immediately 'click' on the captcha image after the page is loaded, so that a captcha is automatically displayed when the page is first loaded, which is convenient for users.

If you use jQuery in your project, you can replacescriptparts with more concise jQuery syntax:

<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>

Save the modified template file.

Step 3: Verify that the captcha function is working

After completing all the above configurations, you can test on the website front-end:

  1. Open your website and visit the page that contains the message form.
  2. Refresh the page, you should see a dynamically generated captcha image and a text box for entering the captcha.
  3. Try clicking the captcha image, the captcha should refresh and display a new image.
  4. Enter the comment content and the correct captcha, then submit. The comment should be successfully submitted.
  5. Try submitting the message again, but this time, deliberately enter an incorrect captcha. The system should prompt that the captcha is incorrect and prevent the message from being submitted.

If everything runs as expected, then congratulations, your AnQiCMS message form captcha function has been successfully enabled and put into use!This adds an important layer of security to the website and greatly improves the content operation environment.


Frequently Asked Questions (FAQ)

1. I have enabled the captcha feature on the backend, but the front-end page does not display. What is the problem?

The captcha is enabled only at the system level, but the display and interaction of the captcha require the cooperation of the front-end template code. Please make sure to check your message form template file (usuallyguestbook/index.html),Confirm that you have correctly added the inclusion according to the second step of the articlecaptcha_id/captchainput boxes and<img>The HTML code for the captcha image and the JavaScript code used for dynamically retrieving and refreshing the captcha.

2. How to solve the problem of not displaying the verification code image or unable to refresh after clicking?

This is usually due to front-end JavaScript code or network request issues. You can try the following troubleshooting steps:

  • Check the browser console:Press F12 to open the browser developer tools and view the "Console" (console) and "Network" (network) tabs.
    • If there is an error in the JavaScript code, the Console will display an error message.
    • In the Network tab, after clicking the captcha image, you should see a link to/api/captchaThe request, check the status code (should be 200 OK) and the response content, to confirm whether it was successfully returned.captcha_idandcaptchaImage path.
  • Check the path:Make sure the path in JavaScript is/api/captchacorrect, without extra slashes or spelling errors.
  • Network issue: Occasionally, it may also be due to slow server response or network connection issues, causing the captcha image to load in a timely manner.

3. What types of captcha are supported by AnQiCMS? Can I change the captcha style?

AnQiCMS built-in captcha function usually provides a common image captcha, which is an interference image composed of numbers and letters.The document does not mention the support for multiple captcha types or the ability to change the built-in captcha style.If you need a more advanced or customized captcha type (such as sliding captcha or point-to-select captcha), it may require secondary development or integration with a third-party captcha service.For most websites, the default picture captcha provided by AnQiCMS is enough to deal with common spam submissions.

Related articles

How to display comment content and user status for the `commentList` label in AnQiCMS?

Build a highly interactive website in AnQiCMS, the comment function is undoubtedly the key to increasing user engagement.The `commentList` tag is a great tool provided by AnQiCMS for this purpose, it can help website developers and operators flexibly display comment content, and clearly present the status of commenters, allowing visitors to grasp the dynamics of the comment area at a glance.### Core Feature Overview: Basic Usage of `commentList` Tag The `commentList` tag is mainly used to retrieve the comment list of a specified document

2025-11-08

How to create a multi-level category navigation and display its subcategories or related documents in AnQiCMS template?

Build flexible multi-level category navigation and content display in AnQiCMS template Effective organization of website content is crucial for providing a good user experience and improving search engine visibility.Whether it is a corporate website, product display, or self-media blog, a clear multi-level classification navigation can help users quickly find the information they need and is also conducive to search engines understanding the structure of the website.AnQiCMS as an efficient and customizable content management system provides flexible template tags, allowing us to easily implement complex multi-level classification navigation and content display. Next

2025-11-08

How to filter the document list based on category ID, model ID, or recommendation attributes for the `archiveList` tag?

In AnQiCMS, flexibly displaying content is one of the keys to building an efficient website.The `archiveList` tag is such a powerful tool that allows us to filter and display the document list of the website based on various conditions.Whether you want to display a certain type of article on a specific page, or organize information based on content type or editor's recommendations, `archiveList` can provide precise control.### Accurate positioning: Filter document list by category ID When we want to display in a certain area of the website, such as the sidebar or a special topic page

2025-11-08

How to get the global configuration information of AnQiCMS template, such as the website name and logo?

In AnQiCMS template, cleverly obtain the global configuration of the website to make your website more flexible When designing or maintaining an AnQiCMS website template, we often encounter a need: to display unified global information on each page of the website, such as the name of the website, logo, filing number, or custom contact information.Manually adding this information to each page is not only inefficient but also time-consuming and laborious to update once it needs to be changed.

2025-11-08

How to use the `archiveFilters` tag in AnQiCMS to achieve combined filtering of document parameters?

## Mastering AnQiCMS: The Art of Using `archiveFilters` Tag for Document Parameter Combination Filtering It is crucial to provide users with accurate and efficient content search experience in content operation.Imagine if your website has a rich variety of content, but users find it difficult to quickly find the information they need. This will undoubtedly greatly affect user experience and the conversion rate of the website.

2025-11-08

How to automatically generate the breadcrumb navigation of the page in the AnQiCMS template using the `breadcrumb` tag?

When browsing websites, we often notice a path information string at the top or bottom of the page, which clearly indicates our current location, such as "Home > Category > Article Title."}This is what we commonly refer to as Breadcrumb Navigation.It not only helps users better understand the website structure and improve the browsing experience, but also has a significant value for search engine optimization (SEO).Why is breadcrumb navigation important? For users, breadcrumb navigation is like a mini-map.

2025-11-08

How to flexibly control the display quantity and style of pagination page numbers with the `pagination` tag of AnQiCMS?

When managing a content-rich website, an efficient page navigation system is crucial for enhancing user experience and search engine optimization (SEO).AnQi CMS knows this, its built-in `pagination` tag provides us with detailed control, making the website page number display both flexible and beautiful.This article will guide you to understand how to use the `pagination` tag, flexibly control the display quantity and style of pagination page numbers.

2025-11-08

How to format timestamp output to date or time string in AnQiCMS template?

In website operation, the date and time information of content publication, update, and other aspects are crucial for user experience and content management.AnQiCMS as a rich-featured content management system, provides flexible template tags, allowing users to easily format the timestamp data stored in the background into various easily readable date or time strings to meet different display requirements.

2025-11-08