PHP之验证码类

Posted 忧郁的小学生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP之验证码类相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/20
 * Time: 14:29
 */

Class captcha{
       //验证码类
     protected $str="xaaxqwe556232assd";  //随机数
     protected $code; //验证码
     protected $length=4; //验证码长度
     protected $width=80;  //验证码宽度
     protected $height=30; //验证码高度
    protected $img;//验证码生成


    //随机数
    public  function getcode()
    {
        $len=strlen($this->str)-1;
        for($i=0;$i<4;$i++)
        {
            $this->code.=$this->str[mt_rand(0,$len)];
        }

    }

    //生成背景
    public function codeBg()
    {
        //新建一个图像
       $this->img=imagecreatetruecolor($this->width,$this->height);
        $color=imagecolorallocate($this->img,rand(1,100),rand(1,100),rand(1,100));  //红,绿,蓝
        //$back=imagecolorallocate($this->img,0,0,0);
        imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color);

    }

    //生成干扰元素

    public function setLine()
    {
        //干扰点
        for($i=0;$i<1000;$i++)
        {
            $color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
            imagesetpixel($this->img,rand(1,99),rand(1,99),$color);
        }
//
//        //干扰线
        for($i=0;$i<5;$i++)
        {
            $color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
            imageline($this->img,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),$color);
        }
//
//        //干扰线




    }

    //生成元素头
    public function outHeader()
    {
        header("Content-type:image/png");
    }

    //写入验证码

    public function writeString()
    {
        $red  = imagecolorallocate ( $this->img ,  255 ,  0 ,  0 );
        imagestring($this->img,5,rand(1,15),rand(1,15),$this->code(),$red);
        //$font  =  ‘arial.ttf‘ ;
        //imagettftext ( $this->img ,  20 ,  0 ,  10 ,  20 ,  $red ,  $font ,  $this->code );
    }
  //shu

    public function png()
    {
        $this->outHeader();
        $this->codeBg();
        $this->getcode();
        $this->setLine();

        $this->writeString();
        session_start();
        $_SESSION[‘code‘]=$this->code();
        imagepng($this->img);
    }
    public function code(){
        return strtoupper($this->code);
    }

}

$ce=new captcha();
$ce->png();
var_dump($_SESSION[‘code‘]);

  测试页面

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<?php   session_start(); ?>
<body>
    <img  title="点击刷新" src="./captcha.class.php" onclick="this.src=‘captcha.class.php?‘+Math.random();"/>
    <form action="<?php $_SERVER[‘PHP_SELF‘];  ?>" method="post">
		<input type="text" name=‘captcha‘ />
		<input type="submit"  name=‘submit‘ value="buton" />
    </form>
</body>
</html>

<?php 
if($_POST[‘submit‘]){
   if($_POST[‘captcha‘]!==$_SESSION[‘code‘])
   {
   	echo "登录失败";
   }else{
   echo "成功";
   }
   }



?>

  

 

以上是关于PHP之验证码类的主要内容,如果未能解决你的问题,请参考以下文章

制作简单安全的php验证码类代码实例

PHP验证码类

php生成验证码类

PHP-解析验证码类--学习笔记

简单实用的PHP验证码类

php验证码类