An explanation of the php Captcha verification code class
Jun 11, 2018 pm 04:44 PM<?php /** Captcha 驗(yàn)證碼類 * Date: 2011-02-19 * Author: fdipzone */ class Captcha{ //class start private $sname = ''; public function __construct($sname=''){ // $sname captcha session name $this->sname?=?$sname==''??'m_captcha'?:?$sname; } /**?生成驗(yàn)證碼圖片 *?@param??int $length?驗(yàn)證碼長(zhǎng)度 *?@param??Array $param??參數(shù) *?@return?IMG */ public?function?create($length=4,$param=array()){ Header("Content-type:?image/PNG"); $authnum?=?$this->random($length); //生成驗(yàn)證碼字符. $width =?isset($param['width'])??$param['width']?:?13; //文字寬度 $height?=?isset($param['height'])??$param['height']?:?18; //文字高度 $pnum =?isset($param['pnum'])??$param['pnum']?:?100; //干擾象素個(gè)數(shù) $lnum =?isset($param['lnum'])??$param['lnum']?:?2; //干擾線條數(shù) $this->captcha_session($this->sname,$authnum); //將隨機(jī)數(shù)寫入session $pw?=?$width*$length+10; $ph?=?$height+6; $im?=?imagecreate($pw,$ph); //imagecreate()?新建圖像,大小為?x_size?和?y_size?的空白圖像。 $black?=?ImageColorAllocate($im,?238,238,238); //設(shè)置背景顏色 $values?=?array( mt_rand(0,$pw),??mt_rand(0,$ph), mt_rand(0,$pw),??mt_rand(0,$ph), mt_rand(0,$pw),??mt_rand(0,$ph), mt_rand(0,$pw),??mt_rand(0,$ph), mt_rand(0,$pw),??mt_rand(0,$ph), mt_rand(0,$pw),??mt_rand(0,$ph) ); imagefilledpolygon($im,?$values,?6,?ImageColorAllocate($im,?mt_rand(170,255),mt_rand(200,255),mt_rand(210,255))); //設(shè)置干擾多邊形底圖 /*?文字?*/ for?($i?=?0;?$i?< strlen($authnum); $i++){ $font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));//設(shè)置文字顏色 $x = $i/$length * $pw + rand(1, 6); //設(shè)置隨機(jī)X坐標(biāo) $y = rand(1, $ph/3); //設(shè)置隨機(jī)Y坐標(biāo) imagestring($im, mt_rand(4,6), $x, $y, substr($authnum,$i,1), $font); } /* 加入干擾象素 */ for($i=0; $i<$pnum; $i++){ $dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //設(shè)置雜點(diǎn)顏色 imagesetpixel($im, mt_rand(0,$pw) , mt_rand(0,$ph) , $dist); } /* 加入干擾線 */ for($i=0; $i<$lnum; $i++){ $dist = ImageColorAllocate($im, mt_rand(50,255),mt_rand(150,255),mt_rand(200,255)); //設(shè)置線顏色 imageline($im,mt_rand(0,$pw),mt_rand(0,$ph),mt_rand(0,$pw),mt_rand(0,$ph),$dist); } ImagePNG($im); //以 PNG 格式將圖像輸出到瀏覽器或文件 ImageDestroy($im); //銷毀一圖像 } /** 檢查驗(yàn)證碼 * @param String $captcha 驗(yàn)證碼 * @param int $flag 驗(yàn)證成功后 0:不清除session 1:清除session * @return boolean */ public function check($captcha,$flag=1){ if(empty($captcha)){ return false; }else{ if(strtoupper($captcha)==$this->captcha_session($this->sname)){ //檢測(cè)驗(yàn)證碼 if($flag==1){ $this->captcha_session($this->sname,''); } return?true; }else{ return?false; } } } /*?產(chǎn)生隨機(jī)數(shù)函數(shù) *?@param int $length 需要隨機(jī)生成的字符串?dāng)?shù) *?@return String */ private?function?random($length){ $hash?=?''; $chars?=?'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789'; $max?=?strlen($chars)?-?1; for($i?=?0;?$i?< $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } /** 驗(yàn)證碼session處理方法 * @param String $name captcha session name * @param String $value * @return String */ private function captcha_session($name,$value=null){ if(isset($value)){ if($value!==''){ $_SESSION[$name] = $value; }else{ unset($_SESSION[$name]); } }else{ return isset($_SESSION[$name])? $_SESSION[$name] : ''; } } } // class end ?>
demo
<? session_start(); require_once('Captcha.class.php'); $obj = new Captcha($sname); # 創(chuàng)建Captcha類對(duì)象 # $sname為保存captcha的session name,可留空,留空則為'm_captcha' $obj->create($length,$param); #?創(chuàng)建Captcha并輸出圖片 #?$length為Captcha長(zhǎng)度,可留空,默認(rèn)為4 /*?$param?=?array( 'width'?=>?13 captcha?字符寬度 'height'?=>?18 captcha?字符高度 'pnum'?=>?100 干擾點(diǎn)個(gè)數(shù) 'lnum'?=>?2 干擾線條數(shù) ) 可留空 */ $obj->check($captcha,$flag); #?檢查用戶輸入的驗(yàn)證碼是否正確,true?or?false #?$captcha為用戶輸入的驗(yàn)證碼,必填 #?$flag?可留空,默認(rèn)為1? # 1:當(dāng)驗(yàn)證成功后自動(dòng)清除captcha?session # 0:當(dāng)驗(yàn)證成功后不清除captcha?session,用於ajax檢查 ?>
This article explains the relevant content about the php Captcha verification code class. For more related knowledge, please pay attention to the php Chinese website.
Related recommendations:
MySQL information_schema related content
View mysql database size, table size and last modification time
Detailed explanation of Sublime Text 2
The above is the detailed content of An explanation of the php Captcha verification code class. For more information, please follow other related articles on the PHP Chinese website!

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

The problem was found in the springboot project production session-out timeout. The problem is described below: In the test environment, the session-out was configured by changing the application.yaml. After setting different times to verify that the session-out configuration took effect, the expiration time was directly set to 8 hours for release. Arrived in production environment. However, I received feedback from customers at noon that the project expiration time was set to be short. If no operation is performed for half an hour, the session will expire and require repeated logins. Solve the problem of handling the development environment: the springboot project has built-in Tomcat, so the session-out configured in application.yaml in the project is effective. Production environment: Production environment release is

Solution to the problem that the php session disappears after refreshing: 1. Open the session through "session_start();"; 2. Write all public configurations in a php file; 3. The variable name cannot be the same as the array subscript; 4. In Just check the storage path of the session data in phpinfo and check whether the sessio in the file directory is saved successfully.

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Problem: Today, we encountered a setting timeout problem in our project, and changes to SpringBoot2’s application.properties never took effect. Solution: The server.* properties are used to control the embedded container used by SpringBoot. SpringBoot will create an instance of the servlet container using one of the ServletWebServerFactory instances. These classes use server.* properties to configure the controlled servlet container (tomcat, jetty, etc.). When the application is deployed as a war file to a Tomcat instance, the server.* properties do not apply. They do not apply,

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

The default expiration time of session PHP is 1440 seconds, which is 24 minutes, which means that if the client does not refresh for more than 24 minutes, the current session will expire; if the user closes the browser, the session will end and the Session will no longer exist.

1. Implementing SMS login based on session 1.1 SMS login flow chart 1.2 Implementing sending SMS verification code Front-end request description: Description of request method POST request path /user/code request parameter phone (phone number) return value No back-end interface implementation: @Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1. Verify mobile phone number if
