开发中的验证码源码

本文介绍了一个简单的PHP验证码生成脚本,该脚本可以生成包含数字的验证码图片,并使用了session来存储验证码的内容。通过随机数生成及图片绘制函数,实现了验证码的动态显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

index.html

<td><input type="text" autocomplete="off" class="myinput" id="code" name="code" style="width:100px;float:left;" />
                <div class="code-img"><img src="yanzhengma.php" alt="看不清换一张" onclick="javascript:this.src='yanzhengma.php?tm='+Math.random()" /></div></td>

yanzhengma.php

<?php

session_start();
IamgeCode(4,100,30);
function IamgeCode($num,$x,$y){
    //生成验证码图片
    Header("Content-type: image/PNG");
    $codenum = ($num=='')? 4 : $num;
    $x_w = ($x=='')? 70 : $x;
    $y_h = ($y=='')? 20 : $y;
    srand((double)microtime()*1000000);//播下一个生成随机数字的种子,以方便下面随机数生成的使用
    //将随机数存入session中
    $_SESSION['authnum']="";
    $im    = imagecreate($x_w,$y_h) or die("Cant's initialize new GD image stream!"); //制定图片背景大小
    $black = ImageColorAllocate($im, 0,0,0); //设定三种颜色
    $white = ImageColorAllocate($im, 255,255,255);
    $gray  = ImageColorAllocate($im, 200,200,200);
    imagefill($im,10,10,$gray); //采用区域填充法,设定(0,0)
    //生成数字和字母混合的验证码方法
    //$ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
    $ychar = "0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9";
    $list  = explode(",",$ychar);
    for($i=0;$i<$codenum;$i++){
        $randnum=rand(0,39);
        $authnum.=$list[$randnum];
    }
    $_SESSION['authnum']=$authnum;
    //imagestring($im,5, 14, 4, $authnum, $black);
    for($k=0;$k<60;$k++) //加入干扰象素
    {
        $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
    }
    for ($i=0;$i<$codenum;$i++)
    {
        $bg_color = imagecolorallocate ($im, rand(0,255), rand(0,128), rand(0,255));
        $x = floor($x_w/$codenum)*$i;
        $y = rand(0,$y_h-15);
        imagechar ($im, 5, $x, $y, $authnum[$i], $bg_color);
    }
    ImagePNG($im);
    ImageDestroy($im);
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值