GD库生成图片验证码-php教程

资源魔 31 0
关于验证码,咱们其实不生疏,随处可见,比方:登录注册、论坛注水、刷票、明码破解等,次要作用是屏蔽机械申请,保证营业没有受机械提交申请滋扰。

上面就来写一个验证码demo,应用最多见的字母加数字验证码,加之滋扰点以及滋扰线,应用的GD库天生的,假如你不装置的话,请自行google装置,另若何判别能否装置启用,请间接正在phpinfo页面搜GD库便可

成果以下图:

5ed6c1cb4851a1d7da179d4b1ffa844.png

前台页面

<?php
if(isset($_REQUEST["code"])){
    session_start();
    if(strtolower($_POST["code"])==$_SESSION["code"]){
        echo "<script>alert('正确!')</script>";
    }else{
        echo "<script>alert('谬误!')</script>";
    }
}
?>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>验证码</title>
    <style>
        #code{
            border: 1px solid #ccc;
            vertical-align: bottom;
        }
        #refresh{
            text-decoration: none;
            font-size: .875em;
        }
    </style>
</head>
<body>
<form action="" method="post">
    <p>
        验证码:
        <img src="code.php?r=<?php echo rand()?>" alt="" id="code">
        <a href="javascript:;" id="refresh">看没有清?</a>
    </p>
    <p>
        输出验证码:
        <input type="text" name="code">
    </p>
    <input type="submit" value="提交">
    <script>
        document.getElementById("code").onclick = document.getElementById("refresh").onclick = refresh;
        function refresh() {
            document.getElementById('code').src='code.php?r='+Math.random()
        }
    </script>
</form>
</body>
</html>

后盾页面

<?php
//启动session
session_start();
$code = "";         //验证码字符串
$str = "qwertyuiopasdfghjklzxcvbnm1234567890";  //验证码字符取值范畴[a-z0-9]
$w = 160;           //图片宽度
$h = 40;            //图片高度
$num = 4;           //验证码字符数
$dotNum = 300;      //滋扰点个数
$lineNum = rand(3, 5);         //滋扰线条数
$font = "./api/DejaVuSansMono.ttf";     //设置字体文件
$image = imagecreatetruecolor($w, $h);  //创立一张指定宽高的图片
$imageColor = imagecolorallocate($image, 255, 255, 255);   //设置布景图片颜色为红色
imagefill($image, 0, 0, $imageColor);  //填充图片布景
//随机验证码,蕴含字母以及数字
for ($i = 0; $i < $num; $i++) {
    $fontColor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));  //天生随机字体颜色
    $content = substr($str, rand(0, strlen($str)), 1);      //随机取字符集中的值
    $code .= $content;
    $fontSize = rand(15, 25);                    //字体巨细
    $x = $i * $w / $num + rand(5, 10);          //指定天生地位X轴偏偏移量
    $y = rand(20, 30);                          //指定天生地位Y轴偏偏移量
    imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $font, $content);
}
$_SESSION["code"] = $code;  //保留验证码字符串到session中
//天生滋扰点
for ($i = 0; $i < $dotNum; $i++) {
    $dotColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $w), rand(0, $h), $dotColor);
}
//天生滋扰线
for ($i = 0; $i < $lineNum; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
    imageline($image, rand(0, $w), rand(0, $h), rand(0, $w), rand(0, $h), $lineColor);
}
header("content-type:image/png");
imagepng($image);
imagedestroy($image);

以上就是GD库天生图片验证码的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 GD库

抱歉,评论功能暂时关闭!