PHP 数字验证码是一种用于验证用户身份或防止自动化程序(如机器人)进行恶意操作的安全措施。它通常由一组随机生成的数字组成,并显示在网页上供用户输入以验证其身份。
以下是一个简单的 PHP 数字验证码生成示例:
<?php
session_start();
// 生成验证码
function generateCaptcha($length = 4) {
$characters = '0123456789';
$captcha = '';
for ($i = 0; $i < $length; $i++) {
$captcha .= $characters[rand(0, strlen($characters) - 1)];
}
return $captcha;
}
// 生成并存储验证码
$captcha = generateCaptcha();
$_SESSION['captcha'] = $captcha;
// 输出验证码图像
header('Content-type: image/png');
$image = imagecreatetruecolor(100, 30);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 100, 30, $bgColor);
imagestring($image, 5, 20, 5, $captcha, $textColor);
imagepng($image);
imagedestroy($image);
?>
通过以上示例代码和相关解释,您可以生成并使用 PHP 数字验证码来提高系统的安全性。
领取专属 10元无门槛券
手把手带您无忧上云