Php 生成验证码(实用)

Php 生成验证码(实用)

php生成验证码案例如下:
/** php 验证码(Session) **/

//判断session是否开启
if(!isset($_SESSION))
{ 
    session_start(); //开启就session
} 

$width=90;  //布画宽度
$height=35; //布画高度
$length=5;  //验证码长度
$code=getcode($length); //获取随机字符串
$_SESSION['verfyCode'] = $code;

$img=imagecreate($width,$height);
$bgcolor=imagecolorallocate($img,240,240,240);
$rectangelcolor=imagecolorallocate($img,150,150,150);
imagerectangle($img,1,1,$width-1,$height-1,$rectangelcolor);//画边框

for($i=0;$i<$length;$i++){
    //循环写字
    $codecolor=imagecolorallocate($img,mt_rand(50,200),mt_rand(50,128),mt_rand(50,200));
    $angle=rand(-20,20);
    $charx=$i*15+8;
    $chary=($height+14)/2+rand(-1,1);
    //imagettftext($img,23,$angle,$charx,$chary,$codecolor,'C:\WINDOWS\Fonts\SIMKAI.TTF',$code[$i]);
    imagettftext($img,23,$angle,$charx,$chary,$codecolor,'../../css/SIMKAI.TTF',$code[$i]);
}

for($i=0;$i<50;$i++){
    //循环画线
    $linecolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
    $linex=mt_rand(1,$width-1);
    $liney=mt_rand(1,$height-1);
    imageline($img,$linex,$liney,$linex+mt_rand(0,4)-2,$liney+mt_rand(0,4)-2,$linecolor);
}

for($i=0;$i<100;$i++){
    //循环画点
    $pointcolor=imagecolorallocate($img,mt_rand(0,250),mt_rand(0,250),mt_rand(0,250));
    imagesetpixel($img,mt_rand(1,$width-1),mt_rand(1,$height-1),$pointcolor);
}

function getcode($length){
    //生成php随机数 
    $pattern = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';//字符池 
    for($i=0;$i<$length;$i++) { 
       $key .= $pattern{mt_rand(0,strlen($pattern))};  
    } 
     
    return $key;
}

ob_clean();
header('Content-type:image/png'); 
imagepng($img);
调用示例如下:
<img id="img" src="source/common/CheckCode.php" alt="" title="看不清?点击更换" style="cursor: pointer;
						width: 90px; height: 30px; margin-bottom: -10px; *margin-bottom: -3px;" οnclick="this.src=this.src+'?'" />
备注:本文经过摘录网上文章修改、整理而成,已记不得原文网址,感谢原创作者。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值