login.php登录页面验证码
时间: 2023-08-16 13:10:09 浏览: 56
您可以使用 PHP GD 库来生成验证码。以下是一个简单的示例:
1. 生成随机字符串
```php
$length = 6; // 验证码长度
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
```
2. 创建验证码图片
```php
$width = 100; // 图片宽度
$height = 30; // 图片高度
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255); // 背景颜色
imagefill($image, 0, 0, $bgColor);
$textColor = imagecolorallocate($image, 0, 0, 0); // 文字颜色
for ($i = 0; $i < $length; $i++) {
$x = ($width - 10) / $length * $i + 5;
$y = $height / 2 + mt_rand(-5, 5);
imagechar($image, 5, $x, $y, $str[$i], $textColor);
}
```
3. 输出验证码图片
```php
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
```
将上述代码保存为 `captcha.php` 文件,并在登录页面中使用 `<img src="captcha.php">` 标签来显示验证码图片即可。在用户提交表单时,验证用户输入的验证码是否正确即可。