PHP驗證碼,php驗證碼代碼_PHP教程

來源:互聯網
上載者:User

PHP驗證碼,php驗證碼代碼


設計一個驗證碼類,在需要的時候可以隨時調用

驗證碼類,儲存為ValidateCode.class.php

 1 php 2 //驗證碼類 3 session_start(); 4 class ValidateCode { 5  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//隨機因子 6  private $code;//驗證碼 7  private $codelen = 4;//驗證碼長度 8  private $width = 130;//寬度 9  private $height = 50;//高度10  private $img;//圖形資源控制代碼11  private $font;//指定的字型12  private $fontsize = 20;//指定字型大小13  private $fontcolor;//指定字型顏色14  //構造方法初始化15  public function __construct() {16   $this->font = './latha.ttf';//注意字型路徑要寫對,否則顯示不了圖片17  }18  //產生隨機碼19  private function createCode() {20   $_len = strlen($this->charset)-1;21   for ($i=0;$i<$this->codelen;$i++) {22    $this->code .= $this->charset[mt_rand(0,$_len)];23   }24  }25  //產生背景26  private function createBg() {27   $this->img = imagecreatetruecolor($this->width, $this->height);28   $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));29   imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);30  }31  //產生文字32  private function createFont() {33   $_x = $this->width / $this->codelen;34   for ($i=0;$i<$this->codelen;$i++) {35    $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));36    imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);37   }38  }39  //產生線條、雪花40  private function createLine() {41   //線條42   for ($i=0;$i<6;$i++) {43    $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));44    imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);45   }46   //雪花47   for ($i=0;$i<100;$i++) {48    $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));49    imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);50   }51  }52  //輸出53  private function outPut() {54  header('Content-type:image/png');55   imagepng($this->img);56   imagedestroy($this->img);57  }58  //對外產生59  public function doimg() {60   $this->createBg();61   $this->createCode();62   $this->createLine();63   $this->createFont();64   $this->outPut();65  }66  //擷取驗證碼67  public function getCode() {68   return strtolower($this->code);69  }70 }

注意:第16行中,要修改字型的路徑,否則字型圖片無法顯示

實現,儲存為captcha.php

1 session_start();2 require './ValidateCode.class.php';  //先把類包含進來,實際路徑根據實際情況進行修改。3 $_vc = new ValidateCode();  //執行個體化一個對象4 $_vc->doimg();  5 $_SESSION['authnum_session'] = $_vc->getCode();//驗證碼儲存到SESSION中

頁面使用

<img  title="點擊重新整理" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();">img>

轉載自:一個漂亮的php驗證碼類(分享)

http://www.bkjia.com/PHPjc/973822.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/973822.htmlTechArticlePHP驗證碼,php驗證碼代碼 設計一個驗證碼類,在需要的時候可以隨時調用 驗證碼類,儲存為ValidateCode.class.php 1 ? php 2 // 驗證碼類 3 session...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.