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

//驗證碼類

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

 }

}

怎麼用PHP寫個驗證碼

首先,當用戶打開頁面時隨機產生一個session,然後根據這個值生成驗證碼圖片。

第二,將驗證碼圖片顯示到表單上。

第三,當用戶提交時表單時,比較session里的值與表單中驗證碼的值進行比較。

簡單的實現過程:

複雜的驗證碼圖片生成:

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

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

效果圖如下:

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

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

相關推薦

  • Python循環符合要求數字求和

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

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

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

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

    編程 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
  • Python中如何判斷字符為數字

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

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

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

    編程 2025-04-29

發表回復

登錄後才能評論