亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

一個(gè)漂亮的php驗(yàn)證碼類(lèi)

時(shí)間:2025-10-06 08:50:17 php語(yǔ)言

一個(gè)漂亮的php驗(yàn)證碼類(lèi)

  編程的魅力在于可以實(shí)現(xiàn)想要的功能,下面小編就為大家分享一個(gè)漂亮的php驗(yàn)證碼類(lèi)。需要的朋友可以過(guò)來(lái)參考下,更多消息請(qǐng)關(guān)注應(yīng)屆畢業(yè)生網(wǎng)。

  直接上代碼,代碼如下:

  /pic/p>

  class ValidateCode {

  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/pic/p>

  private $code;/pic/p>

  private $codelen = 4;/pic/p>

  private $width = 130;/pic/p>

  private $height = 50;/pic/p>

  private $img;/pic/p>

  private $font;/pic/p>

  private $fontsize = 20;/pic/p>

  private $fontcolor;/pic/p>

  /pic/p>

  public function __construct() {

  $this->font = dirname(__FILE__).'/font/elephant.ttf';/pic/p>

  }

  /pic/p>

  private function createCode() {

  $_len = strlen($this->charset)-1;

  for ($i=0;$i<$this->codelen;$i++) {

  $this->code .= $this->charset[mt_rand(0,$_len)];

  }

  }

  /pic/p>

  private function createBg() {

  $this->img = imagecreatetruecolor($this->width, $this->height);

  $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));

  imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);

  }

  /pic/p>

  private function createFont() {

  $_x = $this->width / $this->codelen;

  for ($i=0;$i<$this->codelen;$i++) {

  $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

  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]);

  }

  }

  /pic/p>

  private function createLine() {

  /pic/p>

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

  $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

  imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);

  }

  /pic/p>

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

  $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

  imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);

  }

  }

  /pic/p>

  private function outPut() {

  header('Content-type:image/png');

  imagepng($this->img);

  imagedestroy($this->img);

  }

  /pic/p>

  public function doimg() {

  $this->createBg();

  $this->createCode();

  $this->createLine();

  $this->createFont();

  $this->outPut();

  }

  /pic/p>

  public function getCode() {

  return strtolower($this->code);

  }

  }

  輸出實(shí)例:

  使用方法:

  1、先把驗(yàn)證碼類(lèi)保存為一個(gè)名為 ValidateCode.class.php 的文件;

  2、新建一個(gè)名為 captcha.php 的文件進(jìn)行調(diào)用該類(lèi);

  captcha.php

  代碼如下:

  session_start();

  require './ValidateCode.class.php'; /pic/p>

  $_vc = new ValidateCode(); /pic/p>

  $_vc->doimg();

  $_SESSION['authnum_session'] = $_vc->getCode();/pic/p>

  3、引用到頁(yè)面中,代碼如下:

  代碼如下:

  4、一個(gè)完整的驗(yàn)證頁(yè)面,代碼如下:

  代碼如下:

  session_start();

  /pic/p>

  /pic/p>

  session_destroy();

  /pic/p>

  /pic/p>

  ?>

  此例為session驗(yàn)證實(shí)例

  驗(yàn)證碼:

  /pic/p>

  /pic/p>

  ";

  $validate="";

  if(isset($_POST["validate"])){

  $validate=$_POST["validate"];

  echo "您剛才輸入的是:".$_POST["validate"]."

  狀態(tài):";

  if($validate!=$_SESSION["authnum_session"]){

  /pic/p>

  echo "輸入有誤";

  }else{

  echo "通過(guò)驗(yàn)證";

  }

  }

  ?>

【一個(gè)漂亮的php驗(yàn)證碼類(lèi)】相關(guān)文章:

仿照TP框架自帶的PHP驗(yàn)證碼類(lèi)12-29

php實(shí)現(xiàn)驗(yàn)證碼制作12-24

php如何實(shí)現(xiàn)驗(yàn)證碼03-15

php驗(yàn)證碼代碼怎么寫(xiě)08-30

用php生成帶有雪花背景的驗(yàn)證碼01-08

php生成動(dòng)態(tài)圖片驗(yàn)證碼代碼08-11

PHP學(xué)習(xí):PHP拼音類(lèi)01-25

PHP創(chuàng)建漂亮圖表的步驟12-01

php如何在一個(gè)類(lèi)中引入另外一個(gè)類(lèi)02-13