时光绘梦集
0
位置: 代码 正文

夏夜万年 2024/10/18 周5

php图片验证码

一个简单的php图片验证码,显示两个数字,随机进行加法或者减法运算,计算得到的结果。

结果保存到$_SESSION["yzm"]中,当需要验证时可以进行验证。

图片中另外加入了3条干扰线。

<?php
session_start();
$width = 100; 
$height = 25;
$image = imagecreate($width, $height);
$bg = imagecolorallocate($image, 240, 240, 240);
$color = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $width, $height, $bg);
$num1 = mt_rand(43, 66);
$num2 = mt_rand(10, 33);
$op = mt_rand(0, 1) ? '+' : "-";
$yzm0 = $num1 . $op . $num2 . "=";
$_SESSION["yzm"] = $op == "+" ? $num1 + $num2 : $num1 - $num2;
for ($i = 0; $i < 3; $i++) {
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}
$font_size = 22; 
$current_x = 5;
foreach (str_split($yzm0) as $char) {
    $random_offset = mt_rand(-5, 5);
    imagestring($image, $font_size, $current_x + $random_offset, 5, $char, $color);
    $current_x += (int)($width / 6);
}
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);


上一篇:php便捷连接mysql
下一篇:html代码转义

本文有2条批注

无名小卒
2024/09/22 周7

​<span style='display: inline-flex; align-items: center;'/>验证码:<img src='/yzm'/><input type='number' name='yzm' min='1' max='99'/></span>

使图片和文字居中对齐

无名小卒
2024/09/21 周6

最开始我就是用的那种简单的图片验证码,后来觉得麻烦,感觉没有那种滑块的好,然后自己修修改改了好久,还是不尽人意,不过也算凑活了。

不过好多时候总是验证失败,没办法代码检查太宽松等于没有,紧点自己又验不过,而且就这其实也是能被破解的,只要别人按照我的校验模式去写脚本就可以。

最后发现还是原来的图片文字验证码性价比高,修修改改又改回来了。

    【运行状态】
获取数据中...