溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php中如何生成驗(yàn)證碼

發(fā)布時(shí)間:2021-06-25 12:32:23 來(lái)源:億速云 閱讀:95 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了php中如何生成驗(yàn)證碼,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

image.func.php

<?php 
require_once('string.func.php');
 
 function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){ 
 
  session_start();
  /*定義長(zhǎng)度和寬度*/
$width=80;
$height=30;
/* 創(chuàng)建畫布*/
$image=imagecreatetruecolor($width, $height);
 
/*本函數(shù)用來(lái)匹配圖形的顏色,供其它繪圖函數(shù)使用。參數(shù) image 表示圖形的 handle。參數(shù) red、green、blue 是色彩三原色,其值從 0 至 255....我在此定義黑色和白色*/
$white=imagecolorallocate($image, 255, 255, 255);
$black=imagecolorallocate($image,0,0,0);
 
/*本函數(shù)將圖片的封閉長(zhǎng)方形區(qū)域著色。參數(shù) x1、y1 及 x2、y2 分別為矩形對(duì)角線的坐標(biāo)。參數(shù) col 表示欲涂上的顏色*/
imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);
 
/*buildRandomString函數(shù)用來(lái)生成一個(gè)驗(yàn)證碼*/
$chars=buildRandomString($type,$length);
 
/*將驗(yàn)證碼給session以便用來(lái)判斷用戶輸入是否正確*/
$_SESSION[$sess_name]=$chars;
 
/*定義字體庫(kù)*/
$fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');
 
 
/*用循環(huán)來(lái)將驗(yàn)證碼一個(gè)一個(gè)的寫入圖片中*/
for($i=0;$i<$length;$i++)
{
  $size=mt_rand(14,18);
  $angle=mt_rand(-15,15);
 
  /*驗(yàn)證碼的橫坐標(biāo)與縱坐標(biāo)*/
  $x=5+$i*$size;
  $y=mt_rand(20,26);
 
  $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90));
  $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
  $text=substr($chars,$i,1);
 
  /*本函數(shù)將 TTF (TrueType Fonts) 字型文字寫入圖片*/
 imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
}
if($pixel)
{
for($i=0;$i<50;$i++)
{
  /*本函數(shù)可在圖片上繪出一點(diǎn)。參數(shù) x、y 為欲繪點(diǎn)的坐標(biāo),參數(shù) col 表示該點(diǎn)的顏色*/
  imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black);
}}
if($line)
{
  for($i=0;$i<10;$i++)
  {
  $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90));
  /*畫線段*/
  imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);
}
}
/*以gif形式輸出*/
header("content-type:image/gif");
/*建立GIF圖 并輸出到網(wǎng)頁(yè)*/
imagegif($image);
/*釋放與 image 關(guān)聯(lián)的內(nèi)存*/
imagedestroy($image);
}

string.func.php

<?php 
 
function buildRandomString($type=1,$length=4){
 
if($type==1)
{
  /*join函數(shù)把數(shù)組轉(zhuǎn)換為字符串。。join() 函數(shù)是 implode() 函數(shù)的別名*/
  $chars=join("",range(0,9));
}elseif ($type==2) {
  /*array_merge函數(shù)合并數(shù)組*/
  $chars=join("",array_merge(range("a","z"),range("A","Z")));
}elseif($type==3)
{
  $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
}
 
if($length>strlen($chars))
{
  exit("字符串長(zhǎng)度不夠");
}
/*打亂字符串*/
$chars=str_shuffle($chars);
return substr($chars,0,$length);
 
}
 ?>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php中如何生成驗(yàn)證碼”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI