php验证码实现源码

本文介绍了如何使用PHP创建一个动态验证码系统,包括生成随机验证码、创建图像、防止图像识别攻击的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
session_start();

// 随机码个数
$_rnd_code = 4;

// 创建随机码
global  $_rnum ;
for ($i = 0; $i < 4; $i ++) {
    $_rnum .= dechex(mt_rand(0, 15));
}
// 保存到session中
$_SESSION["code"] = $_rnum;
// echo $_rnum;

// 创建图像
$width = 75;
$height = 25;
$_img = imagecreatetruecolor($width, $height);

// 设置背景色
$_white = imagecolorallocate($_img, 255, 255, 255);
imagefill($_img, 0, 0, $_white);

// 设置边框
$_black = imagecolorallocate($_img, 0, 0, 0);
// imagerectangle($_img, 0, 0, $width - 1, $height - 1, $_black);

// 设置随机线条
for ($i = 0; $i < 6; $i ++) {
    $_rnd_color = imagecolorallocate($_img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imageline($_img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $_rnd_color);
}

// 设置随机雪花
for ($i = 0; $i < 50; $i ++) {
    $_rnd_color = imagecolorallocate($_img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
    imagestring($_img, 1, mt_rand(1, $width), mt_rand(1, $height), '#', $_rnd_color);
}

// 输出验证码
for ($i = 0; $i < strlen($_SESSION['code']); $i ++) {
    $_rnd_color = imagecolorallocate($_img, mt_rand(0, 100), mt_rand(0, 150), mt_rand(0, 200));
    imagestring($_img, 5, $i * $width / $_rnd_code + mt_rand(1, 10), mt_rand(1, $height / 2), $_SESSION['code'][$i], $_rnd_color);
}

// 输出图像
header('Content-Type:image/png');
imagepng($_img);

imagedestroy($_img);

?>


调用:

<IMG src="code.php" id="code" nclick="javascript:this.src='code.php?tm='+Math.random()">


PHP实现验证码通常涉及生成随机字符、图像渲染以及保存验证信息等步骤。这里提供一个简化的实现思路,但请注意这只是一个基础示例,并未涵盖所有安全细节: ```php // 引入GD库用于生成图片 require 'imagecreatetruecolor.php'; // 验证码配置 $length = 6; // 验证码长度 $font_file = 'arial.ttf'; // 字体文件路径 $colors = array('black', 'white'); // 验证码颜色 // 生成验证码字符串 function generate_random_string($length) { $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; return substr(str_shuffle($characters), 0, $length); } // 生成验证码图片 function create_captcha_image($text) { $img_width = 100; $img_height = 40; $captcha_img = imagecreatetruecolor($img_width, $img_height); for ($x = 0; $x < $img_width; $x++) { $color = $colors[array_rand($colors)]; imagesetpixel($captcha_img, $x, mt_rand(0, $img_height - 1), $color); } imagettftext($captcha_img, 20, mt_rand(-5, 5), 10, $img_height / 2, $colors[0], $font_file, $text); return $captcha_img; } // 保存生成的验证码到session或数据库 function save_captcha($text) { $_SESSION['captcha'] = $text; // 可选地,也可以将文本存入数据库供后续验证 // $db->query("INSERT INTO captcha_codes (code) VALUES ('$text')"); } // 主函数 $text = generate_random_string($length); $captcha_img = create_captcha_image($text); save_captcha($text); header('Content-Type: image/png'); imagepng($captcha_img); // 输出图片到浏览器 //
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值