


Sharing examples of generating verification codes in PHP, sharing examples of PHP verification codes_PHP tutorial
Jul 12, 2016 am 08:54 AMSharing examples of php generating verification codes, sharing php verification code examples
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)建畫(huà)布*/ $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)判斷用戶(hù)輸入是否正確*/ $_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è)的寫(xiě)入圖片中*/ 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) 字型文字寫(xiě)入圖片*/ 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)); /*畫(huà)線段*/ 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); } ?>
Articles you may be interested in:
- php image verification code code
- PHP dynamically randomly generated verification code code
- PHP verification code code ( Latest modification, fully customized! )
- A beautiful PHP verification code class (share)
- Solution to "the image cannot be displayed because of its own errors" when PHP generates the verification code
- PHP generates image verification code, click to switch instance
- php generate verification code function
- thinkphp3.2 click refresh to generate verification code
- php generates verification code, shorten Class sharing of thumbnails and watermarks

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
