PHP 验证码生成器:简单易懂的实现代码
以下是一个简单的 PHP 验证码生成程序:
<?php
session_start();
// 生成随机验证码
$code = rand(1000, 9999);
// 将验证码保存到 session 中
$_SESSION['code'] = $code;
// 设置图像大小
$image = imagecreatetruecolor(100, 30);
// 设置背景颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
// 设置文本颜色
$textColor = imagecolorallocate($image, 0, 0, 0);
// 在图像中绘制验证码
imagestring($image, 5, 30, 8, $code, $textColor);
// 输出图像
header('Content-type: image/png');
imagepng($image);
// 释放图像内存
imagedestroy($image);
?>
这个程序生成一个 4 位随机数作为验证码,并将其保存在 session 中。然后创建一个 100x30 的图像,设置背景颜色为白色,文本颜色为黑色,将验证码绘制在图像中,并输出为 PNG 格式的图像。最后释放图像内存。
原文地址: https://www.cveoy.top/t/topic/jM2J 著作权归作者所有。请勿转载和采集!