EMLOG为网站验证码增加干扰线和噪点的办法

反垃圾是几乎所有的站长都会遇到的问题,而对于EMLOG程序来说,官方的EMLOG验证码设计的比较中规中矩,方方正正的很容易被机器识别,因此也就更容易被不法分子爆了。

因此,我们需要做的就是给EMLOG的验证码的识别增加难度,防止机器识别,现在就来为大家分享一下EMLOG网站验证码增加干扰线和噪点的办法。

一、操作步骤

1、打开/include/lib/checkcode.php文件,清空所有代码

2、将下面的代码粘贴到已清空的文件中,上传覆盖

<?php
/**
 * Emlog图片验证码生成程序
 * @copyright (c) Emlog All Rights Reserved
 * Modify By Mrxn 
 * Emlog  Site: http://www.emlog.net/
 * Mrxn's Blog: https://mrxn.net/
 */

session_start();

$randCode = '';
$chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPRSTUVWXYZ23456789';
for ( $i = 0; $i < 5; $i++ ){
    $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}

$_SESSION['code'] = strtoupper($randCode);

$img = imagecreate(75,25) or die("创建图像资源失败,请刷新页面");
$bgColor = isset($_GET['mode']) && $_GET['mode'] == 't' ? imagecolorallocate($img,245,245,245) : imagecolorallocate($img,255,255,255);
$pixColor = imagecolorallocate($img,mt_rand(88, 245), mt_rand(55, 240), mt_rand(99, 200));
//画字符、大小
for($i = 0; $i < 5; $i++){
    $x = $i * 13 + mt_rand(3, 7) - 2;
    $y = mt_rand(0, 3);
    $text_color = imagecolorallocate($img, mt_rand(100, 250), mt_rand(80, 180), mt_rand(90, 220));
    imagechar($img, 5, $x + 5, $y + 3, $randCode[$i], $text_color);
}
//画干扰点
for($j = 0; $j < 240; $j++){
    $x = mt_rand(0,100);
    $y = mt_rand(0,40);
    imagesetpixel($img,$x,$y,$pixColor);
}
//4条横斜线
for ($i=0; $i < 5; $i++) { 
    $lineColor = imagecolorallocate($img, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineX1 = 0;
    $lineX2 = 80;
    $lineY1 = ($i + 1) * 8;
    $lineY2 = ($i + 1) * 15;
    imageline($img, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}

//4条竖斜线
for ($i=0; $i < 5; $i++) { 
    $lineColor = imagecolorallocate($img, rand(50, 150), rand(50, 150), rand(50, 150));
    $lineY1 = 0;
    $lineY2 = 30;
    $lineX1 = ($i + 1) * 8;
    $lineX2 = ($i + 1) * 15;
    imageline($img, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}

header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);

3、重启服务器环境查看效果

二、原理解析

通过代码为验证码增加随机线条,提升文字颜色范围宽度,从而增加验证码识别难度

三、注意事项

1、本教程所使用的教程需要清空官方checkcode.php文件,因此可以直接下载附件中的文件进行替换

2、本教程实际操作后,如果验证码肉眼难以识别,可以通过删除代码的方式适当的降低难度,比如删除噪点、竖斜线等,尺度自己掌握

四、附件下载

提升EMLOG验证码识别难度大小:1M | 密码:无 | 来源:本站下载
版权归属: 博客志
相关推荐

评论区

博主头像
博客志
知道不知道
  • 892

    文章

  • 0

    评论

  • 815110

    浏览