php验证码怎么才能不区分大小写,自动转换小写?

原创 麻木  2020-06-19 20:18:29  阅读 1279 次 评论 0 条
摘要:

php验证码怎么才能不区分大小写,自动转换小写?

用到的php函数strtolower

对存在SESSION内的验证码使用strtolower函数将内容转为小写

$_SESSION['newcode']=strtolower($code);

然后将用户提交的内容同样使用strtolower函数转为小写;最后进行内容正确性判断。

$captcha = strtolower($_POST['newcode']);

完整验证码示例

<?php
//创建背景画布
$img_w = 100;
/*宽*/
$img_h = 30;
*/
$img = imagecreatetruecolor($img_w,$img_h);
$bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc);
imagefill($img,0,0,$bg_color);
//生成验证码
$count = 4;
$code = "";
/*生成的验证码内容范围*/
$charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$str_len = strlen($charset)-1;
/*for循环打印输出验证码*/
for($i=0;$i<$count;$i++){
    $code .=$charset[rand(0,$str_len)];
}

session_start();

  /*strtolower函数将输入的验证码自动转换为小写,用户不需要特意区分大小写*/
$_SESSION['newcode']=strtolower($code);

/*字体大小*/
$font_size = 16;
/*字体文件位置*/
$fontfile = "SourceCodePro-Bold.ttf";
for($i=0;$i<$count;$i++){
    $font_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,50),mt_rand(0,255));
    imagettftext(
    $img,
    $font_size,
    mt_rand(0,20)-mt_rand(0,25),
    $font_size*$i+20,mt_rand($img_h/2,$img_h),
    $font_color,
    realpath($fontfile),
    $code[$i]
    );
}
/*背景干扰点点*/
for($i=0;$i<300;$i++){
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
}
/*干扰线条*/
for($i=0;$i<5;$i++){
    $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imageline($img,mt_rand(0,$img_w),0,mt_rand(0,$img_h),$img_h,$color);
    imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
}
header("content-type:image/png");
imagepng($img);
?>

字体:SourceCodePro-Bold.ttf

点我下字体

登陆界面部分截图

/*判断输入是否为空*/
if (!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['newcode'])) {
    $user = $_POST['username'];
    $passwd = $_POST['password'];
    
    /*strtolower函数将输入的验证码自动转换为小写,用户不需要特意区分大小写*/
    $captcha = strtolower($_POST['newcode']);

输入大写小写都是可以的,不用注意大小写

效果:点我查看(后期可能会失效)

打赏 支付宝打赏 微信打赏
 您阅读本篇文章共花了: 

本文地址:https://www.mamublog.cn/post/156.html
版权声明:本文为原创文章,版权归 mamublog 所有,欢迎分享本文,转载请保留出处!

推荐文章

发表评论


表情

还没有留言,还不快点抢沙发?