TypechoJoeTheme

至尊技术网

统计
登录
用户名
密码
/
注册
用户名
邮箱

PHP动态生成图形验证码

2020-12-15
/
0 评论
/
842 阅读
/
正在检测是否收录...
12/15

1.php

<?php

$w = 80; //设置图片宽和高
$h = 26;
$str = array(); //用来存储随机码
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; //随机挑选其中4个字符,也可以选择更多,注意循环的时候加上,宽度适当调整
for ($i = 0; $i < 4; $i++) {
    $str[$i] = $string[rand(0, 35)];
    $vcode .= $str[$i];
}
session_start(); //启用超全局变量session
$_SESSION["vcode"] = $vcode;
$im = imagecreatetruecolor($w, $h);
$white = imagecolorallocate($im, 255, 255, 255); //第一次调用设置背景色
$black = imagecolorallocate($im, 0, 0, 0); //边框颜色
imagefilledrectangle($im, 0, 0, $w, $h, $white); //画一矩形填充
imagerectangle($im, 0, 0, $w - 1, $h - 1, $black); //画一矩形框
//生成雪花背景
for ($i = 1; $i < 200; $i++) {
    $x = mt_rand(1, $w - 9);
    $y = mt_rand(1, $h - 9);
    $color = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
    imagechar($im, 1, $x, $y, "*", $color);
}
//将验证码写入图案
for ($i = 0; $i < count($str); $i++) {
    $x = 13 + $i * ($w - 15) / 4;
    $y = mt_rand(3, $h / 3);
    $color = imagecolorallocate($im, mt_rand(0, 225), mt_rand(0, 150), mt_rand(0, 225));
    imagechar($im, 5, $x, $y, $str[$i], $color);
}
ob_clean(); //原来的程序没有这一栏
header("Content-type:image/jpeg"); //以jpeg格式输出,注意上面不能输出任何字符,否则出错
imagejpeg($im);
imagedestroy($im);

2.php

<?php

if (!empty($_POST)) {
    session_start();
    if (strtolower($_POST['code']) == strtolower($_SESSION["vcode"])) {
        session_destroy();//彻底销毁session
        echo '验证成功';
    } else {
        echo '验证失败';
    }
} else {
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <title>PHP动态生成图形验证码</title>
</head>
<body>
<form action="" method="post">
    <input type="text" name="code">
    <img src="1.php" onclick="this.src='1.php'" title="点击更换"/>
    <button type="submit">提交</button>
</form>
</body>
</html>
<?php }?>
经验PHP验证码
朗读
赞(0)
版权属于:

至尊技术网

本文链接:

https://www.zzwws.cn/archives/4972/(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. 强强强
    2025-04-07
  2. jesse
    2025-01-16
  3. sowxkkxwwk
    2024-11-20
  4. zpzscldkea
    2024-11-20
  5. bruvoaaiju
    2024-11-14

标签云