


Simple and safe PHP verification code with calling method, PHP verification code with calling_PHP tutorial
Jul 12, 2016 am 08:51 AMSimple and secure PHP verification code with calling method, PHP verification code with calling
1. Verification code example
2. PHP verification code class, secoder.class.php
<?php /** * 安全驗(yàn)證碼 * * 安全的驗(yàn)證碼要:驗(yàn)證碼文字扭曲、旋轉(zhuǎn),使用不同字體,添加干擾碼 * * @author 流水孟春 <cmpan(at)qq.com> * @link http://labs.yulans.cn/YL_Security_Secoder * @link http://wiki.yulans.cn/docs/yl/security/secoder */ class YL_Security_Secoder { /** * 驗(yàn)證碼的session的下標(biāo) * * @var string */ //public static $seKey = 'sid.sek ey.ylans.cn'; public static $seKey = 'sid'; public static $expire = 3000; // 驗(yàn)證碼過(guò)期時(shí)間(s) /** * 驗(yàn)證碼中使用的字符,01IO容易混淆,建議不用 * * @var string */ public static $codeSet = '346789ABCDEFGHJKLMNPQRTUVWXY'; public static $fontSize = 25; // 驗(yàn)證碼字體大小(px) public static $useCurve = true; // 是否畫混淆曲線 public static $useNoise = true; // 是否添加雜點(diǎn) public static $imageH = 0; // 驗(yàn)證碼圖片寬 public static $imageL = 0; // 驗(yàn)證碼圖片長(zhǎng) public static $length = 4; // 驗(yàn)證碼位數(shù) public static $bg = array(243, 251, 254); // 背景 protected static $_image = null; // 驗(yàn)證碼圖片實(shí)例 protected static $_color = null; // 驗(yàn)證碼字體顏色 /** * 輸出驗(yàn)證碼并把驗(yàn)證碼的值保存的session中 * 驗(yàn)證碼保存到session的格式為: $_SESSION[self::$seKey] = array('code' => '驗(yàn)證碼值', 'time' => '驗(yàn)證碼創(chuàng)建時(shí)間'); */ public static function entry() { // 圖片寬(px) self::$imageL || self::$imageL = self::$length * self::$fontSize * 1.5 + self::$fontSize*1.5; // 圖片高(px) self::$imageH || self::$imageH = self::$fontSize * 2; // 建立一幅 self::$imageL x self::$imageH 的圖像 self::$_image = imagecreate(self::$imageL, self::$imageH); // 設(shè)置背景 imagecolorallocate(self::$_image, self::$bg[0], self::$bg[1], self::$bg[2]); // 驗(yàn)證碼字體隨機(jī)顏色 self::$_color = imagecolorallocate(self::$_image, mt_rand(1,120), mt_rand(1,120), mt_rand(1,120)); // 驗(yàn)證碼使用隨機(jī)字體 //$ttf = dirname(__FILE__) . '/ttfs/' . mt_rand(1, 20) . '.ttf'; 4 $ttf = dirname(__FILE__) . '/ttfs/4.ttf'; if (self::$useNoise) { // 繪雜點(diǎn) self::_writeNoise(); } if (self::$useCurve) { // 繪干擾線 self::_writeCurve(); } // 繪驗(yàn)證碼 $code = array(); // 驗(yàn)證碼 $codeNX = 0; // 驗(yàn)證碼第N個(gè)字符的左邊距 for ($i = 0; $i<self::$length; $i++) { $code[$i] = self::$codeSet[mt_rand(0, 27)]; $codeNX += mt_rand(self::$fontSize*1.2, self::$fontSize*1.6); // 寫一個(gè)驗(yàn)證碼字符 imagettftext(self::$_image, self::$fontSize, mt_rand(-40, 70), $codeNX, self::$fontSize*1.5, self::$_color, $ttf, $code[$i]); } // 保存驗(yàn)證碼 isset($_SESSION) || session_start(); $_SESSION[self::$seKey]['code'] = join('', $code); // 把校驗(yàn)碼保存到session $_SESSION[self::$seKey]['time'] = time(); // 驗(yàn)證碼創(chuàng)建時(shí)間 header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); header("content-type: image/png"); // 輸出圖像 imagepng(self::$_image); imagedestroy(self::$_image); } /** * 畫一條由兩條連在一起構(gòu)成的隨機(jī)正弦函數(shù)曲線作干擾線(你可以改成更帥的曲線函數(shù)) * * 高中的數(shù)學(xué)公式咋都忘了涅,寫出來(lái) * 正弦型函數(shù)解析式:y=Asin(ωx+φ)+b * 各常數(shù)值對(duì)函數(shù)圖像的影響: * A:決定峰值(即縱向拉伸壓縮的倍數(shù)) * b:表示波形在Y軸的位置關(guān)系或縱向移動(dòng)距離(上加下減) * φ:決定波形與X軸位置關(guān)系或橫向移動(dòng)距離(左加右減) * ω:決定周期(最小正周期T=2π/∣ω∣) * */ protected static function _writeCurve() { $A = mt_rand(1, self::$imageH/2); // 振幅 $b = mt_rand(-self::$imageH/4, self::$imageH/4); // Y軸方向偏移量 $f = mt_rand(-self::$imageH/4, self::$imageH/4); // X軸方向偏移量 $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期 $w = (2* M_PI)/$T; $px1 = 0; // 曲線橫坐標(biāo)起始位置 $px2 = mt_rand(self::$imageL/2, self::$imageL * 0.667); // 曲線橫坐標(biāo)結(jié)束位置 for ($px=$px1; $px<=$px2; $px=$px+ 0.9) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b $i = (int) ((self::$fontSize - 6)/4); while ($i > 0) { imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這里畫像素點(diǎn)比imagettftext和imagestring性能要好很多 $i--; } } } $A = mt_rand(1, self::$imageH/2); // 振幅 $f = mt_rand(-self::$imageH/4, self::$imageH/4); // X軸方向偏移量 $T = mt_rand(self::$imageH*1.5, self::$imageL*2); // 周期 $w = (2* M_PI)/$T; $b = $py - $A * sin($w*$px + $f) - self::$imageH/2; $px1 = $px2; $px2 = self::$imageL; for ($px=$px1; $px<=$px2; $px=$px+ 0.9) { if ($w!=0) { $py = $A * sin($w*$px + $f)+ $b + self::$imageH/2; // y = Asin(ωx+φ) + b $i = (int) ((self::$fontSize - 8)/4); while ($i > 0) { imagesetpixel(self::$_image, $px + $i, $py + $i, self::$_color); // 這里(while)循環(huán)畫像素點(diǎn)比imagettftext和imagestring用字體大小一次畫出(不用這while循環(huán))性能要好很多 $i--; } } } } /** * 畫雜點(diǎn) * 往圖片上寫不同顏色的字母或數(shù)字 */ protected static function _writeNoise() { for($i = 0; $i < 10; $i++){ //雜點(diǎn)顏色 $noiseColor = imagecolorallocate( self::$_image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) ); for($j = 0; $j < 5; $j++) { // 繪雜點(diǎn) imagestring( self::$_image, 5, mt_rand(-10, self::$imageL), mt_rand(-10, self::$imageH), self::$codeSet[mt_rand(0, 27)], // 雜點(diǎn)文本為隨機(jī)的字母或數(shù)字 $noiseColor ); } } } /** * 驗(yàn)證驗(yàn)證碼是否正確 * * @param string $code 用戶驗(yàn)證碼 * @param bool 用戶驗(yàn)證碼是否正確 */ public static function check($code) { isset($_SESSION) || session_start(); // 驗(yàn)證碼不能為空 if(empty($code) || empty($_SESSION[self::$seKey])) { //echo $_SESSION[self::$seKey]['code'].'1'; return false; } // session 過(guò)期 if(time() - $_SESSION[self::$seKey]['time'] > self::$expire) { unset($_SESSION[self::$seKey]); //echo $_SESSION[self::$seKey]['code'].'2'; return false; //return 0; } // if($code == $_SESSION[self::$seKey]['code']) { if(strtoupper($code) == $_SESSION[self::$seKey]['code']) { //不區(qū)分大小寫比較 //echo $_SESSION[self::$seKey]['code'].'3'; return true; } //echo $_SESSION[self::$seKey]['code'].'4'; return false; } } // useage /* YL_Security_Secoder::$useNoise = false; // 要更安全的話改成true YL_Security_Secoder::$useCurve = true; YL_Security_Secoder::entry(); */ /* // 驗(yàn)證驗(yàn)證碼 if (!YL_Security_Secoder::check(@$_POST['secode'])) { print 'error secode'; } */
3. Calling method
1. Display the verification code page code.php
<?php session_start(); require 'secoder.class.php'; //先把類包含進(jìn)來(lái),實(shí)際路徑根據(jù)實(shí)際情況進(jìn)行修改。 $vcode = new YL_Security_Secoder(); //實(shí)例化一個(gè)對(duì)象 $vcode->entry(); ?>
2. Check whether the verification code is correct
<?php session_start(); require 'secoder.class.php'; //先把類包含進(jìn)來(lái),實(shí)際路徑根據(jù)實(shí)際情況進(jìn)行修改。 $vcode = new YL_Security_Secoder(); //實(shí)例化一個(gè)對(duì)象 //$vcode->entry(); $code = $_GET['code']; echo $vcode->check($code); //$_SESSION['code'] = $vc->getCode();//驗(yàn)證碼保存到SESSION中 ?>
3. Verification code input box call page
<img id="messageImg" src='images/tishis2.gif' width='16' height='16'> 單擊圖片重新獲取驗(yàn)證碼<br> <a href="#"><img src="code.php" onclick="javascript:this.src='code.php?tm='+Math.random();" />
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

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
