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

用到的php函数strtolower

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


//创建背景画布
$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']);

输入大写小写都是可以的,不用注意大小写,均被strtolower函数转换为小写

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

你可能感兴趣的:(php,php验证码)