Home >Backend Development >PHP Tutorial >php图片验证码生成代码

php图片验证码生成代码

WBOY
WBOYOriginal
2016-06-23 13:08:131018browse

首先在index.php页面

    function identity(){         $this->load->view('index',$data);        }            function codes(){        if($_POST['code'] == $_SESSION['code']){            echo 'ok';        }else{            echo 'no';        }     }        function create_code(){       session_start();        //生成验证码图片        ob_clean();         //header("Content-type:image/png");        //全数字        $str="1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f";//要显示的字符,自己可以增加或删除        $list=explode(",",$str);        $r=count($list)-1;        $laststr="";        for($i=0;$i<4;$i++){                $randnum=mt_rand(0,$r);                $laststr .= $list[$randnum];//取出字符,组合成要显示的字符串            }        $_SESSION['code']=$laststr; //将字符串放入SESSION中        //echo $laststr;        $img=imagecreate(28,28);//生成图片        $black=imagecolorallocate($img,0,0,0);  //  设置颜色        $white=imagecolorallocate($img,255,255,255);        $gray=imagecolorallocate($img,200,200,200);        $red=imagecolorallocate($img,255,0,0);        $cl = imagefill($img,0,0,$red);//给图片填充颜色                //将验证法放入图片        imagestring($img,4,10,8,$laststr,$black);//将验证码放到图片上        for ($i=0;$i<8;$i++){                $lineColor        = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));                imageline ($img,rand(0,$x),0,rand(0,$x),$y,$lineColor);        }        //干扰点        for ($i=0;$i<250;$i++){                imagesetpixel($img,rand(0,$x),rand(0,$y),$fontColor);        }        $aa = imagepng($img);        $bb = imagedestroy($img);              //echo $aa;    }

在视图页面index.php

 <form action="/index/codes" method="post">   <label for="yzmai" class="input-tips2">验证码:</label>   <input type="text" name="code"  class="inputstyle2"  maxlength="10"  /><br/>   <img id="code" src="/index/create_code" alt="看不清楚,换一张"   style="max-width:90%" onClick="create_code()"/>   <button type="submit">提交</button> </form><script>function create_code(){    document.getElementById('code').src = '/index/create_code/'+Math.random()*10000;}</script>

可以去尝试下看看

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn