php里验证码怎么打
时间 : 2023-04-26 05:32:02声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

在 PHP 中生成验证码的方法有很多种,下面我会介绍两种最常用的方法。

方法一:使用 GD 库生成图片验证码

1.先创建一个 PHP 文件,命名为 captcha.php:

```php

<?php

session_start();

// 随机生成四位数验证码

$code = rand(1000, 9999);

// 存储验证码到 SESSION

$_SESSION['code'] = $code;

// 设置响应头

header("Content-type: image/png");

// 创建画布

$image = imagecreate(80,40);

// 设置背景颜色

$bg_color = imagecolorallocate($image, 255, 255, 255);

// 设置文本颜色

$text_color = imagecolorallocate($image, 0, 0, 0);

// 在画布上输出验证码

imagestring($image, 10, 20, 10, $code, $text_color);

// 输出图片

imagepng($image);

// 释放内存

imagedestroy($image);

2.在需要使用验证码的页面中,使用以下代码引用 captcha.php 文件:

```php

<img src="captcha.php" alt="验证码">

3.验证用户输入的验证码是否正确:

```php

session_start();

// 判断用户是否提交了表单

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// 获取用户输入的验证码

$code = $_POST['code'];

// 判断验证码是否正确

if (isset($_SESSION['code']) && $code == $_SESSION['code']) {

echo '验证码正确!';

} else {

echo '验证码错误!';

}

// 清空验证码 SESSION

unset($_SESSION['code']);

}

方法二:使用第三方库生成图片验证码

1.在项目中安装验证码生成库,比如使用 Gregwar/Captcha 库:

```bash

composer require gregwar/captcha

2.生成验证码图片和验证码字符串:

```php

use Gregwar\Captcha\CaptchaBuilder;

session_start();

// 生成验证码并存储到 SESSION

$builder = new CaptchaBuilder;

$builder->build();

$_SESSION['phrase'] = $builder->getPhrase();

// 输出图片

header('Content-type: image/jpeg');

$builder->output();

3.在需要使用验证码的页面中,使用以下代码引用 captcha.php 文件:

```php

<img src="captcha.php" alt="验证码">

4.验证用户输入的验证码是否正确:

```php

session_start();

// 判断用户是否提交了表单

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// 获取用户输入的验证码

$code = $_POST['code'];

// 判断验证码是否正确

if (isset($_SESSION['phrase']) && $code == $_SESSION['phrase']) {

echo '验证码正确!';

} else {

echo '验证码错误!';

}

// 清空验证码 SESSION

unset($_SESSION['phrase']);

}

以上就是使用 PHP 生成验证码的两种方法。第一种方法使用了 GD 库来生成验证码图片,需要自己写一些绘图代码;第二种方法使用了第三方库来生成验证码图片和验证码字符串,比较方便。使用哪种方法根据需要来选择即可。

在 PHP 中,你可以通过 GD 库和相关函数来生成验证码。以下是一些示例代码:

```php

<?php

session_start();

// 验证码的宽度和高度

$width = 100;

$height = 40;

// 创建验证码画布

$captcha = imagecreatetruecolor($width, $height);

// 随机生成背景颜色

$bg_color = imagecolorallocate($captcha, rand(200, 255), rand(200, 255), rand(200, 255));

// 绘制背景

imagefill($captcha, 0, 0, $bg_color);

// 随机生成验证码

$captcha_text = ''; // 存储验证码

$captcha_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // 随机字符集

$captcha_length = 6; // 验证码长度

for ($i = 0; $i < $captcha_length; $i++) {

$rand_char = $captcha_chars[rand(0, strlen($captcha_chars) - 1)];

$captcha_text .= $rand_char;

// 每个字符随机生成前景颜色和旋转角度

$char_color = imagecolorallocate($captcha, rand(0, 150), rand(0, 150), rand(0, 150));

$char_angle = rand(-30, 30);

// 绘制字符

imagettftext($captcha, 20, $char_angle, ($i * 16) + 10, 28, $char_color, 'path/to/font.ttf', $rand_char);

}

// 存储验证码到会话

$_SESSION['captcha'] = $captcha_text;

// 输出验证码图片

header('Content-type: image/png');

imagepng($captcha);

imagedestroy($captcha);

?>

这段代码创建了一个宽度为 100,高度为 40 的验证码画布,并在该画布上绘制了随机生成的 6 个字符。然后,将验证码存储到会话中,并输出图片。你可以在 HTML 表单中添加对应的输入框和提交按钮,然后验证用户输入的验证码是否与会话中存储的值相同。