PHP开发 加入小组

298个成员 3006个话题 创建时间:2011-05-30

如何用PHP生成验证码

发表于 2012-04-25 11292 次查看

验证码几乎是网站必备的一个部分,生成验证码无非就那么几个步骤,首先是获取一个随机字符串,然后创建一个布画,将生成的字符串写到布画上,我们还可以在布画上画线画点,可以用随机角度控制斜线的斜率和文字的斜率等,现在帖一短生成验证码的代码。效果还不错。

<?php
if(!isset($_SESSION)){ //判断session是否开启
session_start(); //开启就session
}
$width=70; //布画宽度
$height=25; //布画高度
$length=4;//验证码长度
$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,15,$angle,$charx,$chary,$codecolor,'C:\WINDOWS\Fonts\SIMKAI.TTF',
$code[$i]);
}
for($i=0;$i<20;$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 = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//字符池
for($i=0;$i<$length;$i++) {
$key .= $pattern{mt_rand(0,35)};
}
return $key;

}
ob_clean();
header('Content-type:image/png');
imagepng($img);
?>

效果图如下

7回复
  • 2楼 此情无关风和月 2012-04-25

    看起来还不错啊

  • 3楼 楼兰遗梦 2012-04-25

    我把它整成一个函数,变成自己的,哈哈

  • 4楼 zsmynl 2013-10-11

    支持一个、

  • 5楼 行者_Jerry 2013-10-14

    看来好知网 需要考虑用户贴代码的话,怎么支持代码显示这方面的体验问题,否则好好的代码看起来挺费劲的。

    @知了

  • 6楼 知了 2013-10-14
    @行者_Jerry:看来好知网 需要考虑用户贴代码的话,怎么支持代码显示这方面的体验问题,否则好好的代码看起来挺费劲的。...

    贴代码请在文字样式(左上角段落处)那里选择“预格式文本”

    例如

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

    $width=70; //布画宽度
    $height=25; //布画高度
    $length=4;//验证码长度
    $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,15,$angle,$charx,$chary,$codecolor,'C:\WINDOWS\Fonts\SIMKAI.TTF',
    $code[$i]);
    }
    for($i=0;$i<20;$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 = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';//字符池 
    for($i=0;$i<$length;$i++) { 
    $key .= $pattern{mt_rand(0,35)}; 

    return $key; 

    }
    ob_clean();
    header('Content-type:image/png'); 
    imagepng($img);
    ?>

     

  • 7楼 行者_Jerry 2013-10-14

    @知了  嗯,晓得了,原来这样也可以,明白了。

  • 8楼 山有木兮木有枝8 2016-01-21

    传统字符验证码既不安全,用户体验又差,极验验证的验证码不仅安全性高而且用户体验好,不用自己编写,直接去极验的后台下载一个sdk或者插件安装就好了。http://www.geetest.com/

发表回复
功能维护升级中,维护完成完后将再次开放,非常抱歉给您学习造成的不便。