验证码(CAPTCHA)是一种用于区分人类和计算机的程序,通常用于网站的安全验证。PHP是一种广泛使用的服务器端脚本语言,常用于生成和验证验证码。
以下是一个简单的PHP验证码生成示例,确保字符编码和图像处理库正确配置:
<?php
session_start();
// 生成随机验证码
$code = rand(1000, 9999);
// 将验证码保存到session中
$_SESSION['captcha'] = $code;
// 创建图像
$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, $code, $textColor);
// 输出图像
header('Content-type: image/png');
imagepng($image);
// 销毁图像资源
imagedestroy($image);
?>
phpinfo()
函数检查。通过以上步骤,可以有效解决PHP验证码显示乱码的问题。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云