Home > Article > Backend Development > How to develop verification code function using PHP
How to use PHP to develop verification code function
Verification code (CAPTCHA) is a technical means used to determine whether the user is a real person or a machine. In network applications, verification codes are often used in registration, login, password reset, etc. to prevent malicious machine attacks and illegal logins. This article will introduce how to use PHP to develop verification code functions and provide corresponding code examples.
1. Generate verification code images
In PHP, we can use the GD library to generate verification code images. First, we need to create a PHP file for generating verification code images, such as captcha.php. In this file, we need to implement the following steps:
<?php session_start(); $code = ''; $length = 4; // 验证码长度 $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 验证码字符集 $charsetLen = strlen($charset); for ($i = 0; $i < $length; $i++) { $code .= $charset[mt_rand(0, $charsetLen - 1)]; } $_SESSION['captcha_code'] = $code; // 将验证码保存到session中
$width = 150; // 图片宽度 $height = 50; // 图片高度 $image = imagecreatetruecolor($width, $height); $bgColor = imagecolorallocate($image, 255, 255, 255); // 背景颜色为白色 $textColor = imagecolorallocate($image, 0, 0, 0); // 字体颜色为黑色 imagefill($image, 0, 0, $bgColor);
$fontSize = 20; // 字体大小 $fontAngle = 0; // 字体角度 $x = 30; // 字体X坐标 $y = 30; // 字体Y坐标 imagettftext($image, $fontSize, $fontAngle, $x, $y, $textColor, 'path/to/font.ttf', $code);
$lineColor = imagecolorallocate($image, 200, 200, 200); // 干扰线颜色为灰色 for ($i = 0; $i < 5; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $lineColor); }
header('Content-type: image/png'); // 设置输出类型为PNG图片 imagepng($image); imagedestroy($image);
2. Verify user input
While generating the verification code image, we also need to write code to verify whether the verification code entered by the user is correct. In operations such as login, the verification code entered by the user is usually submitted to the server through a form. We can verify whether the user input is correct by comparing it with the verification code in the session. The following is a simple example:
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $inputCode = $_POST['code']; $captchaCode = $_SESSION['captcha_code']; if (strcasecmp($inputCode, $captchaCode) === 0) { echo '验证通过!'; } else { echo '验证码错误!'; } }
After the user submits the verification code, we can get the verification code entered by the user through $_POST['code'] and pass the strcasecmp() function (case-insensitive) Compare with the verification code in the session. If they are equal, it means that the verification code is passed.
Summary:
This article introduces how to use PHP to develop verification code functions and provides corresponding code examples. By generating verification code images and verifying the verification codes entered by users, we can enhance the security of the website and prevent malicious attacks and illegal logins. In practical applications, we can customize the style, length, character set, etc. of the verification code according to needs. At the same time, in order to improve user experience, we can also add the function of refreshing the verification code to facilitate users to obtain new verification code images.
The above is the detailed content of How to develop verification code function using PHP. For more information, please follow other related articles on the PHP Chinese website!