php驗證碼編寫,php實現驗證碼判斷

本文目錄一覽:

php怎麼編寫手機短信驗證碼功能

以前在遠標做過你的應用應該是這樣吧,用戶輸入手機號碼,點擊發送短信,用戶收到驗證碼,輸入對應的驗證碼 判斷是否正確。

需要:

申請一個短信接口,就是點擊發送驗證碼的時候,提交到接口給該號碼下發驗證碼。

技術方面的實現:

1、點擊獲取驗證碼

2、程序ajax post提交到短信接口

3、短信接口服務商 接口判斷用戶和口令,正確後,下發短信給該號碼。

4、用戶輸入號碼,程序判斷驗證碼是否一致。

怎樣製作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如何實現驗證碼?許昌鯉魚IT計算機電腦軟件編程培訓中心

驗證碼在表單實現越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實現的驗證碼。好吧,其實是沒有事情干,但是又不想浪費時間,所以學習了下php實現驗證碼。正所謂,技多不壓身。而且,也可以封裝成一個函數,以後使用的時候也是很方便的,當然現在未封裝。

現在來說說簡單的純數字驗證碼吧。

如果是初學者,建議按照我代碼的注釋 //數字 一步步來。最簡單的方法,還是把整個代碼複製走了。

新建一個captcha.php:

php //10設置session,必須處於腳本最頂部

session_start(); $image = imagecreatetruecolor(100, 30); //1設置驗證碼圖片大小的函數

//5設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域着色,col 表示欲塗上的顏色

imagefill($image, 0, 0, $bgcolor); //10設置變量

$captcha_code = “”; //7生成隨機數字

for($i=0;$i4;$i++){ //設置字體大小

$fontsize = 6;

//設置字體顏色,隨機顏色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色

//設置數字

$fontcontent = rand(0,9); //10.=連續定義變量

$captcha_code .= $fontcontent;

//設置坐標

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION[‘authcode’] = $captcha_code; //8增加干擾元素,設置雪花點

for($i=0;$i200;$i++){ //設置點的顏色,50-200顏色比數字淺,不干擾閱讀

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 畫一個單一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干擾元素,設置橫線

for($i=0;$i4;$i++){ //設置線的顏色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2設置頭部,image/png

header(‘Content-Type: image/png’); //3imagepng() 建立png圖形函數

imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image

imagedestroy($image);

接着就是靜態頁的代碼了:index.html

doctype htmlhtml

head

meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″

title確認驗證碼title

head

body

form method=”post” action=”./form.php”

p驗證碼: img id=”captcha_img” border=’1′ src=’./captcha.php?r=echo rand(); ?’ style=”width:100px; height:30px” / a href=”javascript:void(0)” onclick=”document.getElementById(‘captcha_img’).src=’./captcha.php?r=’+Math.random()”換一個?a

p

P請輸入驗證碼:input type=”text” name=’authcode’ value=”/p

pinput type=’submit’ value=’提交’ style=’padding:6px 5px;’/p

bodyhtml

從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:

php header(“Content-Type:text/html;charset=utf-8”); //設置頭部信息

//isset()檢測變量是否設置

if(isset($_REQUEST[‘authcode’])){ session_start(); //strtolower()小寫函數

if(strtolower($_REQUEST[‘authcode’])== $_SESSION[‘authcode’]){ //跳轉頁面

echo “script language=\”javascript\””; echo “document.location=\”./form.php\””; echo “/script”;

}else{ //提示以及跳轉頁面

echo “script language=\”javascript\””; echo “alert(‘輸入錯誤!’);”; echo “document.location=\”./form.php\””; echo “/script”;

} exit();

}

那麼,純數字的實現了,數字加英文的也應該不難了。要修改的代碼 只是在 captcha.php 將 //7生成隨機數字 修改成 //7生成隨機的字母和數字,如果你真的很可愛的就修改這幾個字就認為可以實現的話,那麼祝賀你,你永遠保持快樂。腦殘兒童歡樂多。

廢話不多說了,拉代碼吧。

php //10設置session,必須處於腳本最頂部

session_start(); $image = imagecreatetruecolor(100, 30); //1設置驗證碼圖片大小的函數

//5設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域着色,col 表示欲塗上的顏色

imagefill($image, 0, 0, $bgcolor); //10設置變量

$captcha_code = “”; //7生成隨機的字母和數字

for($i=0;$i4;$i++){ //設置字體大小

$fontsize = 8;

//設置字體顏色,隨機顏色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色

//設置需要隨機取的值,去掉容易出錯的值如0和o

$data =’abcdefghigkmnpqrstuvwxy3456789′; //取出值,字符串截取方法 strlen獲取字符串長度

$fontcontent = substr($data, rand(0,strlen($data)),1); //10.=連續定義變量

$captcha_code .= $fontcontent;

//設置坐標

$x = ($i*100/4)+rand(5,10); $y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

} //10存到session

$_SESSION[‘authcode’] = $captcha_code; //8增加干擾元素,設置雪花點

for($i=0;$i200;$i++){ //設置點的顏色,50-200顏色比數字淺,不干擾閱讀

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 畫一個單一像素

imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);

} //9增加干擾元素,設置橫線

for($i=0;$i4;$i++){ //設置線的顏色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線

imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);

} //2設置頭部,image/png

header(‘Content-Type: image/png’); //3imagepng() 建立png圖形函數

imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image

imagedestroy($image);

其他的兩個頁面,不許要修改。

一般而言,現在就已經夠用了。但是就像動漫一樣,總會有番外。

那麼,我們來個漢字的番外吧。其實我也準備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習js的。

而且,還可以和答辯的老師說,我們驗證碼不需要素材,連圖片也是生成的,用自己的知識裝13,也沒有設么的。

php //11設置session,必須處於腳本最頂部

session_start(); //1設置驗證碼圖片大小的函數

$image = imagecreatetruecolor(200, 60);

//5設置驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);

$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff

//6區域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的區域着色,col 表示欲塗上的顏色

imagefill($image, 0, 0, $bgcolor); //7設置ttf字體

$fontface = ‘FZYTK.TTF’; //7設置字庫,實現簡單的數字儲備

$str=’天地不仁以萬物為芻狗聖人不仁以百姓為芻狗這句經常出現在控訴暴君暴政上地殘暴不仁把萬物都當成低賤的豬狗來看待而那些高高在上的所謂聖人們也沒兩樣還不是把我們老百姓也當成豬狗不如的東西但實在正取的解讀是地不情感用事對萬物一視同仁聖人不情感用事對百姓一視同仁執子之手與子偕老當男女主人公含情脈脈看着對方說了句執子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執子之手與子偕老於嗟闊兮不我活兮於嗟洵兮不我信兮意思是說戰士之間的約定說要一起死現在和我約定的人都走了我怎麼活啊赤裸裸的兄弟江湖戰友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧’; //str_split()切割字符串為一個數組,一個中文在utf_8為3個字符

$strdb = str_split($str,3);

//11

$captcha_code = ”; //8生成隨機的漢子

for($i=0;$i4;$i++){ //設置字體顏色,隨機顏色

$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色

//隨機選取中文

$in = rand(0,count($strdb)); $cn = $strdb[$in]; //將中文記錄到將保存到session的字符串中

$captcha_code .= $cn; /*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,

string $fontfile ,string $text ) 幕布 ,尺寸,角度,坐標,顏色,字體路徑,文本字符串

mt_rand()生成更好的隨機數,比rand()快四倍*/

imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);

} //11存到session

$_SESSION[‘authcode’] = $captcha_code; //9增加干擾元素,設置點

for($i=0;$i200;$i++){ //設置點的顏色,50-200顏色比數字淺,不干擾閱讀

$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 畫一個單一像素

imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);

} //10增加干擾元素,設置線

for($i=0;$i4;$i++){ //設置線的顏色

$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //設置線,兩點一線

imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);

} //2設置頭部,image/png

header(‘Content-Type: image/png’); //3imagepng() 建立png圖形函數

imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image

imagedestroy($image);

其他的頁面也是不需要修改的。

效果圖如下:

php怎麼實現驗證碼的

驗證碼功能機制實現思路

常規的驗證碼實現:

a、產生一張png的圖片

b、為圖片設置背景色

c、設置字體顏色和樣式

d、產生4位數的隨機的驗證碼

e、把產生的每個字符調整旋轉角度和位置畫到png圖片上

f、加入噪點和干擾線防止註冊機器分析原圖片來惡意註冊

g、輸出圖片

h、釋放圖片所佔內存

i、將驗證碼保存到session或是數據庫

j、將和輸入的驗證碼進行對比

短信(郵箱)驗證碼機制:

a、產生4-6位數的隨機的驗證碼

b、把產生的每個字符保存到session或是數據庫

c、將驗證碼發送到用戶的手機(郵箱)

d、用戶在規定時間內進行輸入

e、將驗證碼從session或是數據庫中取出

f、將和輸入的驗證碼進行對比驗證

請問PHP生成驗證碼的類怎麼寫?

?php

class Code{

// 1. 定義各個成員 有寬、高、畫布、字數、類型、畫類型

private $width; //寬度

private $height; //高度

private $num; //驗證碼字數

private $imgType; //生成圖片類型

private $Type; //字串類型 1,2,3 三個選項 1 純數字 2 純小寫字母 3 大小寫數字混合

private $hb; //畫布

public $codestr; // 驗證碼字串

public function __construct($height=20,$num=4,$imgType=”jpeg”,$Type=1){

$this-width = $num*20;

$this-height = $height;

$this-num = $num;

$this-imgType = $imgType; 

$this-Type = $Type; 

$this-codestr = $this-codestr();

$this-zuhe();

}

// 2. 定義隨機獲取字符串函數

private function codestr(){

switch($this-Type){

case 1: // 類型為1 獲取1-9隨機數

$str = implode(“”,array_rand(range(0,9),$this-num));

break;

case 2: // 類型為2 獲取a-z隨機小寫字母

$str = implode(“”,array_rand(array_flip(range(a,z)),$this-num));

break;

case 3: // 類型為3 獲取數字,小寫字母,大寫字母 混合

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

$m = rand(0,2);

switch($m){

case 0:

$o = rand(48,57);

break;

case 1:

$o = rand(65,90);

break;

case 2:

$o = rand(97,122);

break; 

}

$str .= sprintf(“%c”,$o);

}

break; 

}

return $str; 

}

// 3. 初始化畫布圖像資源

private function Hb(){

$this-hb = imagecreatetruecolor($this-width,$this-height); 

}

// 4. 生成背景顏色

private function Bg(){

return imagecolorallocate($this-hb,rand(130,250),rand(130,250),rand(130,250)); 

}

// 5. 生成字體顏色

private function Font(){

return imagecolorallocate($this-hb,rand(0,100),rand(0,100),rand(0,100)); 

}

// 6. 填充背景顏色

private function BgColor(){

imagefilledrectangle($this-hb,0,0,$this-width,$this-height,$this-Bg()); 

}

// 7. 干擾點

private function ganrao(){

$sum=floor(($this-width)*($this-height)/3);

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

imagesetpixel($this-hb,rand(0,$this-width),rand(0,$this-height),$this-Bg()); 

}

}

// 8. 隨機直線 弧線

private function huxian(){

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

imageArc($this-hb,rand(0,$this-width),rand(0,$this-height),rand(0,$this-width),rand(0,$this-height),rand(0,360),rand(0,360),$this-Bg()); 

}

// 9. 寫字

private function xiezi(){

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

$x=ceil($this-width/$this-num)*$i; 

$y=rand(1,$this-height-15);

imagechar($this-hb,5,$x+4,$y,$this-codestr[$i],$this-Font());

}

// 10. 輸出

private function OutImg(){

$shuchu=”image”.$this-imgType; 

$header=”Content-type:image/”.$this-imgType;

if(function_exists($shuchu)){

header($header);

$shuchu($this-hb); 

}else{

exit(“GD庫沒有此類圖像”); 

}

}

// 11. 拼裝

private function zuhe(){

$this-Hb();

$this-BgColor();

$this-ganrao();

$this-huxian();

$this-xiezi();

$this-OutImg(); 

}

public function getCodeStr(){

return $this-codestr; 

}

}

$a = new Code();

$a -getCodeStr();

?

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

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

運行截圖:

望採納,謝謝

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-24 16:27
下一篇 2024-11-24 16:27

相關推薦

  • PHP和Python哪個好找工作?

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

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

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

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若服務器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台服務器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種數據庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論