PHP验证码

来源:互联网 发布:司令部升级数据 编辑:程序博客网 时间:2024/05/17 02:12
 

<?php

Header("Content-type: image/PNG");

//初始化

$border = 1;                             //是否要边框 1:0不要

$how = 5;                                     //验证码位数

$w = $how*13;                                //图片宽度

$h = 20;                                  //图片高度

$fontsize = 5;                               //字体大小

$array="0123456789";                     //随机字符

$seccode = "";                           //验证码字符串初始化

srand((double)microtime()*1000000);       //初始化随机数种子

$im = ImageCreate($w, $h);               //创建验证图片

//绘制基本框架

$bgcolor = ImageColorAllocate($im, 255, 255, 255);    //设置背景颜色

ImageFill($im, 0, 0, $bgcolor);                            //填充背景色

if($border) {

     $black = ImageColorAllocate($im, 0, 0, 0);            //设置边框颜色

     ImageRectangle($im, 0, 0, $w-1, $h-1, $black);   //绘制边框

}

 

//逐位产生随机字符

for($i=0; $i<$how; $i++) {  

     $code = substr($array,rand(0,strlen($array)-1),1);                                        //取字符

     $j = !$i ? 4 : $j+12;                                                                           //绘字符位置

     $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));          //字符随即颜色

     ImageChar($im, $fontsize, $j, 3, $code, $color3);                                        //绘字符

     $seccode .= $code;                                                                              //逐位加入验证码字符串

}

 

//添加干扰

for($i=0; $i<5; $i++) {                                                                                                 //绘背景干扰线

     $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));                            //干扰线颜色

     ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, $color1);        //干扰线

}  

for($i=0; $i<$how*40; $i++){                                                                                           //绘背景干扰点  

     $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));                            //干扰点颜色

     ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干扰点

}

//把验证码字符串写入session

session_start();

$_SESSION['seccode'] = $seccode;

/*绘图结束*/

Imagegif($im);

ImageDestroy($im);

/*绘图结束*/

?>

 

验证页面

<?php

session_start();

$type=$_POST['type'];

if($type=="seccode") {

     if($_SESSION['seccode']==$_POST['SEC']) {

         echo "验证码正确";

     } else {

         echo "验证码错误";

     }

} elseif($type=="Username") {

     if($_POST['SEC']=="a") {

         echo "用户名正确";

     } else {

         echo "用户名错误";

     }

} elseif($type=="Password") {

     if($_POST['SEC']=="b") {

         echo "密码正确";

     } else {

         echo "密码错误";

     }

}

?>

 

调用

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Demo</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <style type="text/css">

        .checkcode

        {

            cursor: pointer;

        }

    </style>

    <script type="text/javascript" src="cc.js"></script>

    <script type="text/javascript">

        function check(pp,type) {

             Ajax.sendurl="d.php?authnum="+Math.random();

             Ajax.method="post";

             Ajax.contentid="Err";

             Ajax.loadingid="Err";

             Ajax.loading='数据加载中,请稍候...'

             Ajax.sendstr="type="+type+"&SEC="+document.getElementById(pp).value;

             Ajax.send();

        }

        function seccode() {

              check("SEC","seccode");

        }

        function username() {

              check("USer","Username");

        }

        function password() {

              check("Pass","Password");

        }

    </script>

</head>

<body>

     <div id="Err"></div>

     <br />

    <img src="c.php?authnum=123456789" onclick="this.src='c.php?authnum='+Math.random()"

        alt="如果不认识点击图片刷新" class="checkcode" /><br />

    <input type="text" id="SEC" onblur="seccode();" />

    <button id="SD" onclick="seccode();">

        Check</button>

    <br />

    <input type="text" id="USer" onblur="username();" />

    <button id="SD" onclick="username();">

        Check</button>

    <input type="text" id="Pass" onblur="password();" />

    <button id="SD" onclick="username();">

        Check</button>

</body>

</html>

 

 

 

原创粉丝点击