国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php驗證碼生成程序代碼

2024-09-04 11:44:27
字體:
來源:轉載
供稿:網友

本文章給大家介紹利用session存儲與gd庫一并生成驗證碼程序,同時會加入一些干擾元素,這樣就可以簡單的防機器注冊了,下面我來給各位同學介紹介紹.

PHP驗證碼并生成圖片程序,采用了session識別,稍微改進了一下目前網絡上流傳的PHP驗證碼,加入雜點,數(shù)字顏色隨機顯示,控制4位數(shù)字顯示,話不多說了,程序如下,分享出來.

新建yz.php驗證碼生成文件,以下代碼需要打開php的GD庫,修改php.in文件的配置,把已經注釋掉的行之前的分號取消即可:extension=php_gd2.dll,代碼如下:

  1. <?php 
  2.    class ValidationCode 
  3.    { 
  4.     //屬性 
  5.     private $width
  6.     private $height
  7.     private $codeNum
  8.        private  $image
  9.     private $disturbColorNum;  //干擾元素數(shù)目 
  10.     private  $checkCode
  11.     function __construct($width=80,$height=20,$codeNum=4) 
  12.      { 
  13.      $this->width=$width
  14.      $this->height=$height
  15.      $this->codeNum=$codeNum
  16.      $number=floor($width*$height/15); 
  17.      if($number>240-$codeNum
  18.     { 
  19.       $this->disturbColorNum=240-$codeNum
  20.      }else 
  21.       { 
  22.       $this->disturbColorNum=$number
  23.       } 
  24.       $this->checkCode=$this->createCheckcode(); 
  25.     } 
  26.     function getCheckCode() 
  27.     { 
  28.            return $this->checkCode; 
  29.     } 
  30.     private function createImage(){ 
  31.           $this->image=imagecreatetruecolor($this->width,$this->height); 
  32.     $backcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(255,255)); 
  33.     imagefill($this->image,0,0,$backcolor); 
  34.     $border=imagecolorallocate($this->image,0,0,0); 
  35.     imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 
  36.     } 
  37.     private function setDisturbColor(){ 
  38.      for($i=0;$i<$this->disturbColorNum;$i++){ 
  39.       $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); 
  40.      imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 
  41.      } 
  42.      for($i=0;$i<10;$i++) 
  43.      { 
  44.                   $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); 
  45.       imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,300),55,44,$color);  
  46.      } 
  47.     } 
  48.       private function outputText($fontFace=""){ 
  49.     for($i=0;$i<$this->codeNum;$i++) 
  50.     { 
  51.      $fontcolor=imagecolorallocate($this->image,rand(0,128),rand(0,128),rand(0,128)); 
  52.     if($fontFace==""
  53.    { 
  54.      $fontsize=rand(3,5); 
  55.      $x=floor($this->width/$this->codeNum)*$i+5; 
  56.      $y=rand(0,$this->height-15); 
  57.      imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor); 
  58.     } 
  59.     else 
  60.    { 
  61.      $fontsize=rand(12,16); 
  62.      $x=floor(($this->width-8)/$this->codeNum)*$i+8; 
  63.      $y=rand($fontsize,$this->height-8); 
  64.      imagettftext($this->image,$fontsize,rand(-45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i}); 
  65.     } 
  66.     } 
  67.    } 
  68.  
  69.    private function createCheckCode(){ 
  70.     $code="23456789abcdefghijkmnpqrstuvwrst"
  71.     $str=""
  72.     for($i=0;$i<$this->codeNum;$i++) 
  73.     { 
  74.      $char=$code{rand(0,strlen($code)-1)}; 
  75.      $str.=$char
  76.     } 
  77.     return $str
  78.    } 
  79.    private function outputImage() 
  80.     { 
  81.     if(imagetypes()&IMG_GIF) 
  82.      { 
  83.        header("Content-Type:image/gif"); 
  84.         imagepng($this->image); 
  85.      }else if(imagetypes()&IMG_JPG) 
  86.      { 
  87.         header("Content-Type:image/jpeg"); 
  88.         imagepng($this->image); 
  89.      }else if(imagetypes()&IMG_PNG) 
  90.      { 
  91.         header("Content-Type:image/png"); 
  92.       imagepng($this->image); 
  93.      }else if(imagetypes()&IMG_WBMP){ 
  94.                  header("Content-Type:image/vnd.wap.wbmp"); 
  95.      imagepng($this->image); 
  96.      }else 
  97.      { 
  98.       die("PHP不支持圖片驗證碼"); 
  99.      } 
  100.     } 
  101.         //通過該方法向瀏覽器輸出圖像 
  102.     function  showImage($fontFace=""
  103.     { 
  104.      //創(chuàng)建圖像背景 
  105.             $this->createImage(); 
  106.      //設置干擾元素 
  107.            $this->setDisturbColor(); 
  108.      //向圖像中隨機畫出文本 
  109.      $this->outputText($fontFace); 
  110.      //輸出圖像 
  111.      $this->outputImage(); 
  112.     } 
  113.  
  114.     function __destruct() 
  115.     { 
  116.      imagedestroy($this->image); 
  117.     } 
  118.    } 
  119.    function checklogin(){ 
  120.         if(emptyempty($_POST['name'])) 
  121.                 die'用戶名不能為空'); 
  122.     if(emptyempty($_POST['password'])) 
  123.      die("密碼不能為空"); 
  124.     if($_SESSION['code']!=$_POST['vertify']) 
  125.      die("驗證碼輸入不正確".$_SESSION['code']); 
  126.     
  127.     $username=$_POST['name']; 
  128.     $password=md5($_POST['password']); 
  129.     //檢查是否存在 
  130.          conndb($username,$password); 
  131.    } 
  132.    function conndb($name="",$ps=""){ 
  133.         $conn=mysql_connect('localhost','root','123456'); 
  134.        if(!$conndie("數(shù)據庫連接失敗".mysql_error()); 
  135.      mysql_select_db('5kan',$connor die('選擇數(shù)據庫失敗'.mysql_error()); 
  136.   mysql_set_charset('utf8',$conn); 
  137.   $sql="select id from k_user where  username='{$name}' and password='{$ps}'"
  138.   $result=mysql_query($sqlor die("SQL語句錯誤".mysql_error()); 
  139.   if(mysql_num_rows($result)>0)  die("登錄成功"); 
  140.   else  die("用戶名或者密碼錯誤"); 
  141.   mysql_close($conn); 
  142.    } 
  143.     session_start(); 
  144.    if(!isset($_POST['randnum'])) 
  145.    {//開源代碼Vevb.com 
  146.      $code=new ValidationCode(120,20,4); 
  147.      $code->showImage("comicbd.ttf");  //顯示在頁面 
  148.   $_SESSION['code']=$code->getCheckCode();//保存在服務器中 
  149.    } 
  150.    else 
  151.    { 
  152.     checklogin(); 
  153.    } 
  154.  
  155. ?> 

到具體調用的地方,用這樣的形式:<img src="/yz.php" align="absmiddle" />就可以了;驗證的時候驗證session:$_SESSION['VCODE']的值就可以了,還可以對以上代碼稍微改進,改成兩個數(shù)字相加求和的形式.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南岸区| 宿州市| 建宁县| 四川省| 甘谷县| 新乡市| 盐山县| 通榆县| 商丘市| 桂阳县| 文安县| 蓬安县| 监利县| 张掖市| 丰城市| 高州市| 邹平县| 勃利县| 晋城| 明溪县| 洛南县| 封丘县| 沧源| 军事| 历史| 静乐县| 屏东市| 行唐县| 佛冈县| 泉州市| 渑池县| 桦甸市| 维西| 兰考县| 甘肃省| 南漳县| 溧阳市| 肃宁县| 汤阴县| 通河县| 扬州市|