製作php驗證碼(php製作數字驗證碼)

本文目錄一覽:

php驗證碼怎麼實現

1. 新建code.php驗證碼生成文件

在此之前必須打開php的GD庫,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分號。代碼如下:

?php

session_start();

//生成驗證碼圖片

Header(“Content-type: image/PNG”);

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位數字

for($i=0;$i4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i100;$i++) //加入干擾象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION[‘Checknum’] = $vcodes;

?

2. 顯示驗證碼圖片

在需要顯示驗證碼的頁面中加入

input type=”text” name=”passcode”

img src=”code.php”

3.判斷並獲取驗證碼的值

驗證碼是通過第一步驟代碼中的$_SESSION[‘Checknum’] = $vcodes;賦的值,所以驗證碼的值存在$_SESSION[‘Checknum’]當中。在驗證頁面,使用以下代碼,

session_start();//啟動會話

$code=$_POST[“passcode”];

if( $code == $_SESSION[“Checknum”])

{…}即可完成驗證碼登錄。

運行截圖:

望採納,謝謝

怎樣製作PHP驗證碼

?php

//驗證碼:文本類型為圖像

header(“content-type:image/png”);

define(‘TYPE’,3);//1.字母 2.字母數字 3.數字 4.邏輯 5.漢字

session_start();

//創建畫布

$img = imagecreatetruecolor(90,33);

//創建顏色

//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));

$bgcolor = imagecolorallocate($img,255,255,255);

$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));

//填充顏色到畫布

imagefill($img,0,0,$bgcolor);

//創建但像素的點為干擾項

//for($i=0;$i100;$i++){

// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));

// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);

//}

//

////劃線

//$linecolor = imagecolorallocate($img,255,0,0);

//imageline($img,0,0,70,30,$linecolor);

//

////多邊形

// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );

// imagepolygon ( $img ,

// array (

// 5 , 5,

// 5, 15 ,

// 20,15,

// 20,5

// ),

// 4 ,

// $col_poly );

////弧線

//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );

//imagearc($img,35,15,30,30,0,360,$arcColor);

//創建驗證碼的內容

//#字母

$letter = range(‘A’,’Z’);

$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];

//數字字母

$num = range(0,9);

$numberAndLetter = array_merge($letter,$num);

$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];

//#數字

$number = rand(1000,9999);

//#邏輯

$x = rand(1,9);

$y = rand(1,9);

$expression = $x.”+”.$y.”=?”;

$sum = $x+$y;

//#漢字

$CH = array(‘恭喜發財’,’財源滾滾’,’財源廣進’,’才高八斗’,’學富五車’,’抬頭見喜’);

$chstr = $CH[rand(0,count($CH)-1)];

switch(TYPE){

case 1 : imagettftext($img,14,0,7,23,$textcolor,’MSYH.TTF’,$letterStr);$_SESSION[‘code’]=$letterStr;break;

case 2 : imagettftext($img,14,0,7,23,$textcolor,’MSYH.TTF’,$nal);$_SESSION[‘code’]=$nal;break;

case 3 : imagettftext($img,14,0,13,23,$textcolor,’MSYH.TTF’,$number);$_SESSION[‘code’]=$number;break;

case 4 : imagettftext($img,14,0,7,23,$textcolor,’MSYH.TTF’,$expression);$_SESSION[‘code’]=$sum;break;

case 5 : imagettftext($img,11,0,6,21,$textcolor,’MSYHBD.TTF’,$chstr);$_SESSION[‘code’]=$chstr;break;

}

//輸入圖像到瀏覽器

imagepng($img);

?

怎樣用PHP製作驗證碼

?php

//驗證碼類

class ValidateCode {

 private $charset = ‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789’;//隨機因子

 private $code;//驗證碼

 private $codelen = 4;//驗證碼長度

 private $width = 90;//寬度

 private $height = 40;//高度

 private $img;//圖形資源句柄

 private $font;//指定的字體

 private $fontsize = 20;//指定字體大小

 private $fontcolor;//指定字體顏色

 //構造方法初始化

 public function __construct() {

  $this-font = dirname(__FILE__).’/font/elephant.ttf’;//注意字體路徑要寫對,否則顯示不了圖片

 }

 //生成隨機碼

 private function createCode() {

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

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

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

  }

 }

 //生成背景

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

 }

 //生成文字

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

  }

 }

 //生成線條、雪花

 private function createLine() {

  //線條

  for ($i=0;$i6;$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);

  }

  //雪花

  for ($i=0;$i100;$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);

  }

 }

 //輸出

 private function outPut() {

  header(‘Content-type:image/png’);

  imagepng($this-img);

  imagedestroy($this-img);

 }

 //對外生成

 public function doimg() {

  $this-createBg();

  $this-createCode();

  $this-createLine();

  $this-createFont();

  $this-outPut();

 }

 //獲取驗證碼

 public function getCode() {

  return strtolower($this-code);

 }

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297897.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python循環符合要求數字求和

    這篇文章將詳細介紹如何通過Python循環符合要求數字求和。如果你想用Python求和但又不想手動輸入數字,那麼本文將是一個不錯的選擇。 一、使用while循環實現求和 sum =…

    編程 2025-04-29
  • Python基本數字類型

    本文將介紹Python中基本數字類型,包括整型、布爾型、浮點型、複數型,並提供相應的代碼示例以便讀者更好的理解。 一、整型 整型即整數類型,Python中的整型沒有大小限制,所以可…

    編程 2025-04-29
  • Python列印數字三角形

    本文將詳細闡述如何使用Python列印數字三角形,包括從基本代碼實現到進階操作的應用。通過本文的學習,您可以掌握Python的基礎語法,同時加深對Python循環和函數的理解,提高…

    編程 2025-04-29
  • Python數字求和怎麼寫

    在Python中實現數字求和非常簡單,下面將從多個方面對Python數字求和的實現方法做詳細的闡述。 一、直接使用「+」符號進行求和 a = 10 b = 20 c = a + b…

    編程 2025-04-29
  • Python提取連續數字

    本文將介紹如何使用Python提取一個字元串中的連續數字。 一、使用正則表達式提取 正則表達式是一種可以匹配文本片段的模式。Python內置了re模塊,可以使用正則表達式進行字元串…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • Python中如何判斷字元為數字

    判斷字元是否為數字是Python編程中常見的需求,本文將從多個方面詳細闡述如何使用Python進行字元判斷。 一、isdigit()函數判斷字元是否為數字 Python中可以使用i…

    編程 2025-04-29
  • Python如何將字元串1234變成數字1234

    Python作為一種廣泛使用的編程語言,對於數字和字元串的處理提供了很多便捷的方式。如何將字元串「1234」轉化成數字「1234」呢?下面將從多個方面詳細闡述Python如何將字元…

    編程 2025-04-29
  • Python實現統計100以內能被7整除的數字個數

    本文將從以下幾個方面詳細闡述如何使用Python來實現統計100以內能被7整除的數字個數。具體內容包括: 一、range函數 Python中的range函數是用來生成一個數字序列的…

    編程 2025-04-28

發表回復

登錄後才能評論