PHP生成数字验证码
侧边栏壁纸
  • 累计撰写 17 篇文章
  • 累计收到 1 条评论

PHP生成数字验证码

赖毓强
2023-05-11 / 0 评论 / 308 阅读 / 正在检测是否收录...

在网站的登陆注册、数据校验等页面,有些时候为了防止接口请求过多和防止机器人,通常使用到验证码,下面使用php生成

<?php
error_reporting(0);
    session_start();  
    
    //创建一张宽100高30的图像  
    $image = imagecreatetruecolor(100, 30);  
    
    //为$image设置背景颜色为白色  
    $bgcolor = imagecolorallocate($image, 255, 255, 255);  
    
    //填充背景颜色  
    imagefill($image, 0, 0, $bgcolor);  
  
    //生成4个随机数  
/*  for($i=0; $i<4; $i++){ 
        //设置字体为6 
        $fontsize=6; 
        //设置背景颜色为随机颜色 三个rand()函数分别对应颜色的rgb让他们产生在0~120这个范围的数值 
        $fontcolor=imagecolorallocate($image, rand(0,120), rand(0, 120), rand(0,120)); 
        //生成随机数字 
        $fontcontent=rand(0, 9); 
        //控制数字出现的位置x->left y->top 
        $x=($i*100/4)+rand(5, 10); 
        $y=rand(5, 10); 
 
        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); 
 
    } 
*/  
    $captch_code="";  
    for($i=0; $i<4; $i++){  
        $fontsize=52;  
        $fontcolor=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0, 120));  
        //$data="1234567890abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
        $data="1234567890";
        //设置每次产生的字符从$data中每次截取一个字符  
        $fontcontent=substr($data, rand(0,strlen($data)), 1);
        //让产生的四个字符拼接起来  
        $captch_code.=$fontcontent;  
        //控制每次出现的字符的坐标防止相互覆盖即x->left y->top  
        $x=($i*100/4)+rand(5, 10);  
        $y=rand(5, 10);  
        //此函数用来将产生的字符在背景图上画出来  
        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);  
    }  
    $_SESSION['code']=$captch_code;//把产生的验证码存入session中
    //echo $_SESSION['token_code'];
    //用来在背景图片上产生200个干扰点  
    for($i=0; $i<90; $i++){  
        //干扰点的颜色  
        $pointcolor=imagecolorallocate($image, rand(50,200), rand(50, 200), rand(50, 200));  
        //该函数用来把每个干扰点在背景上描绘出来  
        imagesetpixel( $image, rand(1, 99), rand(1,29), $pointcolor);  
    }  
  
    //产生三条干扰线  
    for ($i=1; $i <4 ; $i++) {   
        # code...  
        //干扰线的颜色  
        $linecolor=imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));  
        //画出每条干扰线  
        imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1,29), $linecolor);  
    }  
    //设置header图片格式为png  
    //header('content-type:image/png');  
    //显示图片
    $time = date('Y-m-d-H:i:s').".".rand(1000000000,9999999999);
    $mcs = "code".$time.".png";
    $img = imagepng($image,$mcs);
        unlink($mcs);
?> 

创建一个名为code.php的文件,访问即可生成验证码。

需要验证用户提交的是否正确,只需要判断用户提交的验证码和$_SESSION['code']是否一致即可

1

评论 (0)

取消