»ùÓÚPHPʵÏÖÓû§×¢²áµÇ¼¹¦ÄÜ

5年以前  |  阅读数:986 次  |  编程语言:PHP 

±¾ÎĽeÉܵÄÊÇ»uÓÚPHPʵÏÖÓû§×¢²aµÇ¼¹¦ÄÜ£¬±¾ÏiÄ¿ÖΪËIJ¿ÖÄÚÈÝ£º1Ç°¶ËÒ³ÃaeÖÆ×÷£¬2ÑeÖ¤ÂeÖÆ×÷£¬3ʵÏÖ×¢²aµÇ½£¬4¹¦ÄÜÍeÉÆ¡£¾ßÌaÇe¿o¿ÉÒÔÍuÏ¿´¡£

ÑeÖ¤ÂeÖÆ×÷

Ò»¡¢ÊµÑe¼o½e

±¾´ÎʵÑe½«»a´øÁi´o¼ÒʹÓÃÃaeÏo¶ÔÏoµÄ˼Ïe*a×°Ò»¸oÑeÖ¤ÂeÀa¡£²¢ÔÚ×¢²aºÍµÇ½½çÃaeչʾʹÓá£Í¨¹ý±¾´ÎʵÑeµÄѧϰ£¬Äa½«»aÁiÎoµ½ PHP µÄ OOP ˼Ïe£¬ÒÔ¼° GD ¿aµÄʹÓã¬ÑeÖ¤ÂeÉu³É¡£

1.1 Éae¼°µ½µÄ֪ʶµa

  • PHP
  • GD¿a
  • OOP±a³Ì

1.2 ¿ª*¢¹¤¾ß

sublime£¬Ò»¸o½±a¿iËÙµÄÎı¾±a¼­Æ÷¡£µa»÷×ÀÃae×oϽǣº Ó¦ÓóÌÐo²Ëµ¥/¿ª¢/sublime

¶þ¡¢*a×°ÑeÖ¤ÂeÀa

2.1 ½¨Á¢Ä¿Â¼ÒÔ¼°×¼±¸×ÖÌa

ÔÚ web Ŀ¼Ï½¨Á¢Ò»¸o admin Ŀ¼×÷ΪÎÒÃǵĺǫĿ¼£¬´aeźǫ´uÂeÎļþ¡£ÔÚ admin Ͻ¨Á¢Ò»¸o fonts Ŀ¼£¬ÓÃÓÚ´aeÅÖÆ×÷ÑeÖ¤ÂeËuÐe×ÖÌa¡£

ÔÚ admin ÏÂн¨Ò»¸o Captcha.php Îļþ£¬Õa¾ÍÊÇÎÒÃÇÐeÒª±a¼­µÄÑeÖ¤ÂeÀaÎļþ¡£

µ±Ç°Ä¿Â¼²a´Î½a¹¹£º

±a¼­ Captcha.php Îļþ£º


    <?php 
    /**
    * Captcha class
    */
    class Captcha
    {

      function __construct()
      {
        # code...
      }
    }

Ìi¼Ó¸ÃÀaµÄ˽ÓÐÊoÐԺ͹¹Ôi½¨£º


    <?php 
    /**
    * Captcha class
    */
    class Captcha
    {
      private $codeNum;  //ÑeÖ¤ÂeλÊý
      private $width;  //ÑeÖ¤ÂeͼƬ¿i¶È
      private $height;  //ÑeÖ¤ÂeͼƬ¸ß¶È
      private $img;  //ͼÏñ×ÊÔ´¾a±u
      private $lineFlag;  //ÊÇ*ñÉu³É¸ÉÈÅÏßÌo
      private $piexFlag;  //ÊÇ*ñÉu³É¸ÉÈŵa
      private $fontSize;  //×ÖÌa´oС
      private $code;  //ÑeÖ¤Âe×Ö*u
      private $string;  //Éu³ÉÑeÖ¤ÂeµÄ×Ö*u¼¯
      private $font;  //×ÖÌa
      function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
      {
        $this->string = 'qwertyupmkjnhbgvfcdsxa123456789';  //È¥³ýÒ»Ð(C)Ïa½uµÄ×Ö*u
        $this->codeNum = $codeNum;
        $this->height = $height;
        $this->width = $width;
        $this->lineFlag = $lineFlag;
        $this->piexFlag = $piexFlag;
        $this->font = dirname(__FILE__).'/fonts/consola.ttf';
        $this->fontSize = $fontSize;
      }
    }

×ÖÌaÎļþ¿Éͨ¹ýÒÔÏÂÃuÁiÏÂÔص½ fonts Ŀ¼£º

$ wget http://labfile.oss.aliyuncs.com/courses/587/consola.ttf

½ÓÏÂÀ´¿ªÊ¼±aд¾ßÌaµÄ½¨£º

´´½¨Í¼Ïñ×ÊÔ´¾a±u


    //´´½¨Í¼Ïñ×ÊÔ´  
    public function createImage(){
        $this->img = imagecreate($this->width, $this->height);  //´´½¨Í¼Ïñ×ÊÔ´
        imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));  //Ìi³aͼÏñ±³¾°£¨Ê¹ÓÃdzɫ£(C)
      }

Óõ½µÄÏa¹Øº¯Êý

  • imagecreate£ºÐ½¨Ò»¸o»uÓÚµ÷É«°aµÄͼÏñ
  • imagecolorallocate£ºÎªÒ»uͼÏñÖÅaÑÕÉ«
  • mt_rand£ºÉu³É¸uºÃµÄËae»uÊý

´´½¨ÑeÖ¤Âe×Ö*u´®²¢Êa³oµ½Í¼Ïñ


    //´´½¨ÑeÖ¤Âe  
    public function createCode(){
        $strlen = strlen($this->string)-1;
        for ($i=0; $i < $this->codeNum; $i++) { 
          $this->code .= $this->string[mt_rand(0,$strlen)];  //´Ó×Ö*u¼¯ÖÐËae»uÈ¡³oËĸo×Ö*uÆ´½Ó
        }
         $_SESSION['code'] = $this->code;  //¼ÓÈe session ÖÐ

       //¼ÆËaÿ¸o×Ö*u¼a¾a
        $diff = $this->width/$this->codeNum;
        for ($i=0; $i < $this->codeNum; $i++) { 
              //Ϊÿ¸o×Ö*uÉu³ÉÑÕÉ«£¨Ê¹ÓÃÉiÉ«£(C)
         $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
         //дÈeͼÏñ
          imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
        }
      }

Óõ½µÄÏa¹Øº¯Êý

  • imagecreate£ºÐ½¨Ò»¸o»uÓÚµ÷É«°aµÄͼÏñ
  • imagecolorallocate£ºÎªÒ»uͼÏñÖÅaÑÕÉ«
  • mt_rand£ºÉu³É¸uºÃµÄËae»uÊý

´´½¨ÑeÖ¤Âe×Ö*u´®²¢Êa³oµ½Í¼Ïñ


    //´´½¨ÑeÖ¤Âe  
    public function createCode(){
        $strlen = strlen($this->string)-1;
        for ($i=0; $i < $this->codeNum; $i++) { 
          $this->code .= $this->string[mt_rand(0,$strlen)];  //´Ó×Ö*u¼¯ÖÐËae»uÈ¡³oËĸo×Ö*uÆ´½Ó
        }
         $_SESSION['code'] = $this->code;  //¼ÓÈe session ÖÐ

       //¼ÆËaÿ¸o×Ö*u¼a¾a
        $diff = $this->width/$this->codeNum;
        for ($i=0; $i < $this->codeNum; $i++) { 
              //Ϊÿ¸o×Ö*uÉu³ÉÑÕÉ«£¨Ê¹ÓÃÉiÉ«£(C)
         $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
         //дÈeͼÏñ
          imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
        }
      }

Óõ½µÄÏa¹Øº¯Êý£º

  • imagettftext£ºÓà TrueType ×ÖÌaÏoͼÏñдÈeÎı¾

´´½¨¸ÉÈÅÏßÌo


    //´´½¨¸ÉÈÅÏßÌo£¨Ä¬ÈÏËÄÌo£(C)
    public function createLines(){
        for ($i=0; $i < 4; $i++) { 
          $color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));  //ʹÓÃdzɫ
          imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); 
        }
      }

Óõ½µÄÏa¹Øº¯Êý£º

  • imageline£º»­Ò»ÌoÏ߶Î

´´½¨¸ÉÈŵa


    //´´½¨¸ÉÈŵa £¨Ä¬ÈÏÒ»°Ù¸oµa£(C)
    public function createPiex(){
        for ($i=0; $i < 100; $i++) { 
          $color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
          imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
      }

ʹÓõÄÏa¹Øº¯Êý£º

  • imagesetpixel£º»­Ò»¸oµ¥Ò»ÏñËØ

¶ÔÍaÊa³oͼÏñ£º


     public function show()
      {
        $this->createImage();
        $this->createCode();
        if ($this->lineFlag) {  //ÊÇ*ñ´´½¨¸ÉÈÅÏßÌo
          $this->createLines();
        }
        if ($this->piexFlag) {  //ÊÇ*ñ´´½¨¸ÉÈŵa
          $this->createPiex();
        }
        header('Content-type:image/png');  //ÇeÇoÒ³ÃaeµÄÄÚÈÝÊÇpng¸ñʽµÄͼÏñ
        imagepng($this->img);  //ÒÔpng¸ñʽÊa³oͼÏñ
        imagedestroy($this->img);  //Ça³ýͼÏñ×ÊÔ´£¬ÊÍ*ÅÄÚ´ae
      }

Óõ½µÄÏa¹Øº¯Êý£º

  • imagepng£ºÒÔ PNG ¸ñʽ½«Í¼ÏñÊa³oµ½a¯ÀÀÆ÷»oÎļþ
  • imagedestroy£ºÏu»ÙһͼÏñ

¶ÔÍaÌa¹(C)ÑeÖ¤Âe£º


    public function getCode(){
        return $this->code;
      }
    ÍeÕu´uÂeÈçÏ£º
    <?php 
    /**
    * Captcha class
    */
    class Captcha
    {
      private $codeNum;
      private $width;
      private $height;
      private $img;
      private $lineFlag;
      private $piexFlag;
      private $fontSize;
      private $code;
      private $string;
      private $font;
      function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
      {
        $this->string = 'qwertyupmkjnhbgvfcdsxa123456789';
        $this->codeNum = $codeNum;
        $this->height = $height;
        $this->width = $width;
        $this->lineFlag = $lineFlag;
        $this->piexFlag = $piexFlag;
        $this->font = dirname(__FILE__).'/fonts/consola.ttf';
        $this->fontSize = $fontSize;
      }

      public function createImage(){
        $this->img = imagecreate($this->width, $this->height);
        imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
      }

      public function createCode(){
        $strlen = strlen($this->string)-1;
        for ($i=0; $i < $this->codeNum; $i++) { 
          $this->code .= $this->string[mt_rand(0,$strlen)];
        }
        $_SESSION['code'] = $this->code;
        $diff = $this->width/$this->codeNum;
        for ($i=0; $i < $this->codeNum; $i++) { 
          $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
          imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
        }
      }

      public function createLines(){
        for ($i=0; $i < 4; $i++) { 
          $color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
          imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); 
        }
      }

      public function createPiexs(){
        for ($i=0; $i < 100; $i++) { 
          $color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
          imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
      }

      public function show()
      {
        $this->createImage();
        $this->createCode();
        if ($this->lineFlag) {
          $this->createLines();
        }
        if ($this->piexFlag) {
          $this->createPiexs();
        }
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
      }

      public function getCode(){
        return $this->code;
      }
    }

ÒÔÉϾÍÊÇÑeÖ¤ÂeÀaµÄÈ«²¿´uÂe¡£¿´ÆðÀ´Èʵͦ¼oµ¥µÄ£¬²»¹ýÓõÄͼÏñ´¦Àiº¯Êý±È½Ï¶a£¬ÉÏÃaeÏa¹ØµÄº¯ÊýÎÒÒ²×oÁ˱ØÒªµÄÁ´½ÓºÍÓÃ;˵Ã÷¡£ÕaÐ(C)º¯ÊýÒ²²»ÓÃËÀ¼ÇÓ²±³£¬Óoµ½²»Ça³þµÄ£¬Ëaeʱ²eÔÄ PHP ¹Ù½Îĵµ£¬×iÖØÒªµÄÊÇ»¹ÓÐÖÐÎÄÎĵµ¡£

2.2 ʹÓÃÑeÖ¤Âe

¼ÈÈ»ÒѾ­a×°Íe±Ï£¬ÄǾͿÉÒÔ¿ªÊ¼Ê¹ÓÃÁË¡£ÕaÀiΪÁ˽±a£¬Ö±½ÓÔÚ Captcha ÀaµÄÏÂ*½µ÷ÓøÃÀa£º


    session_start(); //¿ªÆosession
    $captcha = new Captcha();  //ʵÀý»¯ÑeÖ¤ÂeÀa(¿É×Ô¶¨Òa²ÎÊý)
    $captcha->show();  //µ÷ÓÃÊa³o

Èý¡¢Ç°¶Ëչʾ

ºo¶ËÒѾ­×¼±¸ºÃÁËÑeÖ¤Âe£¬Ç°¶Ë½çÃae¾Í¿ÉÒÔչʾÁË£¬ÐÞ¸Ä index.php ÖеÄ×¢²aÓeµÇ½±iµ¥µÄÑeÖ¤Âe²¿*Ö£º


    <div class="form-group">
     <div class="col-sm-12">
       <img src="admin/Captcha.php" alt="" id="codeimg" onclick="javascript:this.src = 'admin/Captcha.php?'+Math.random();">
       <span>Click to Switch</span>
     </div>
    </div>

img ±eÇ(C)Ìi¼ÓÁ˵a»÷ʼþµÄ js ´uÂe£¬ÕaÑu¾Í¿ÉÒÔʵÏÖµa»÷¸u»»ÑeÖ¤ÂeµÄ¹¦ÄÜ£¡

Ч¹uͼ£º

ËÄ¡¢ÍeÉÆ

µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃǵÄÑeÖ¤ÂeÄ£¿e»u±¾¾ÍÍe³ÉÁË¡£Ñ§Ï°µ½ÕaÀi£¬´o¼ÒÓ¦¸Ã¶ÔÃaeÏo¶ÔÏo±a³ÌÓÐÁ˽øÒ»²½µÄÀi½a¡£Ò²ÁiÎoµ½ÁËһ˿ OOP ˼Ïe¡£OOP µÄÈý´oÌØÕ÷£ºa×°£¬¼Ì³Ð£¬¶a̬¡£ÎÒÃÇÕaÀiÖ»Óõ½ÁËÒ»µaa×°µÄ˼Ïe¡£´o¼Ò¿ÉÒÔ¼ÌÐøÍeÉƺ͸ĽøÕa¸oÑeÖ¤ÂeÀa£¬Ée¼Æ³o¸u¼ÓÍeÃÀµÄÀa¡£Õa¸oʵÑeÒ²¸aeËßÎÒÃÇ£¬PHP µÄº¯ÊýºÜ¶a£¬²»ÒªËÀ¼ÇÓ²±³£¬¶a¿´¹Ù*½Îĵµ¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例