Current Location: Home> Latest Articles> How to Generate CAPTCHA Images with Chinese Characters Using PHP to Enhance Website Security

How to Generate CAPTCHA Images with Chinese Characters Using PHP to Enhance Website Security

M66 2025-06-19

How to Generate CAPTCHA Images with Chinese Characters Using PHP

With the rapid development of the internet, CAPTCHA has become a widely used security mechanism for websites and applications. While traditional CAPTCHAs usually use English characters and numbers, sometimes we need to generate CAPTCHAs with Chinese characters. This article will explain how to generate CAPTCHA images with Chinese characters using PHP, and it provides specific code examples.

Step 1: Prepare the Chinese Character Set

To generate CAPTCHA images with Chinese characters, you first need to prepare a set of Chinese characters. You can choose a common set of Chinese characters according to your needs. In this article, we will use a selection of common Chinese characters and store them in an array to use as the CAPTCHA character set.
<?php
$chineseChars = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '百', '千', '万', '亿', '天', '地', '王', '赵', '钱', '孙');
?>

Step 2: Generate Random CAPTCHA

Next, we need to randomly select a certain number of characters from the Chinese character set to form the CAPTCHA. Here, we will generate a CAPTCHA with 3 characters. Below is the code to generate the random CAPTCHA:
<?php
$code = ''; // Initialize CAPTCHA as an empty string

for ($i = 0; $i < 3; $i++) {
    $index = mt_rand(0, count($chineseChars) - 1); // Randomly select a character
    $code .= $chineseChars[$index]; // Append the character to the code
}
?>

Step 3: Generate CAPTCHA Image

To generate the CAPTCHA image, we need to use PHP's GD library. First, we will create a canvas, then set its width, height, and background color. After that, we will draw the CAPTCHA text on the canvas and output the image to the browser or save it as a file. Here's the code:
<?php
$width = 120; // Canvas width
$height = 40; // Canvas height

// Create the canvas
$image = imagecreate($width, $height);

// Set the background color
$bgColor = imagecolorallocate($image, 255, 255, 255); // White background

// Randomly choose a color for the CAPTCHA text
$textColor = imagecolorallocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));

// Path to the Chinese font
$fontFile = 'path/to/chinese_font.ttf';

// Draw the CAPTCHA text
imagettftext($image, 20, 0, 10, 30, $textColor, $fontFile, $code);

// Output the CAPTCHA image to the browser
header('Content-Type: image/png');
imagepng($image);

// Destroy the image resource
imagedestroy($image);
?>

Step 4: Use the CAPTCHA Image

After generating the CAPTCHA image, you can display it on your website's registration, login, or password recovery pages. The user needs to enter the CAPTCHA to complete the verification process. Below is the code to store the CAPTCHA in a session and use it in a form:
<?php
session_start();

// Store the CAPTCHA in the session
$_SESSION['captcha'] = $code;
?>

You can then embed the CAPTCHA image in an HTML form for the user to enter the code:

<form action="verify.php" method="post">
    <input type="text" name="code" placeholder="Please enter the CAPTCHA">
    <button type="submit">Submit</button>
</form>

Conclusion

Through this article, you have learned how to generate CAPTCHA images with Chinese characters using PHP. The process includes preparing the character set, generating random CAPTCHA, using the GD library to generate the CAPTCHA image, and implementing the CAPTCHA on your website's registration or login pages. By following these simple steps, you can easily add CAPTCHA functionality to your website and enhance its security.