Home Backend Development PHP Tutorial A PHP verification code generation code

A PHP verification code generation code

Jul 25, 2016 am 08:43 AM

  1. class ValidationCode
  2. {
  3. //属性
  4. private $width;
  5. private $height;
  6. private $codeNum;
  7. private $image;
  8. private $disturbColorNum; //干扰元素数目
  9. private $checkCode;
  10. function __construct($width=80,$height=20,$codeNum=4)
  11. {
  12. $this->width=$width;
  13. $this->height=$height;
  14. $this->codeNum=$codeNum;
  15. $number=floor($width*$height/15);
  16. if($number>240-$codeNum)
  17. {
  18. $this->disturbColorNum=240-$codeNum;
  19. }else
  20. {
  21. $this->disturbColorNum=$number;
  22. }
  23. $this->checkCode=$this->createCheckcode();
  24. }
  25. function getCheckCode()
  26. {
  27. return $this->checkCode;
  28. }
  29. private function createImage(){
  30. $this->image=imagecreatetruecolor($this->width,$this->height);
  31. $backcolor=imagecolorallocate($this->image,rand(225,255),rand(225,255),rand(255,255));
  32. imagefill($this->image,0,0,$backcolor);
  33. $border=imagecolorallocate($this->image,0,0,0);
  34. imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
  35. }
  36. private function setDisturbColor(){
  37. for($i=0;$i<$this->disturbColorNum;$i++){
  38. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  39. imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  40. }
  41. for($i=0;$i<10;$i++)
  42. {
  43. $color=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
  44. imagearc($this->image,rand(-10,$this->width),rand(-10,$this->height),rand(30,300),rand(20,300),55,44,$color);
  45. }
  46. }
  47. private function outputText($fontFace=""){
  48. for($i=0;$i<$this->codeNum;$i++)
  49. {
  50. $fontcolor=imagecolorallocate($this->image,rand(0,128),rand(0,128),rand(0,128));
  51. if($fontFace=="")
  52. {
  53. $fontsize=rand(3,5);
  54. $x=floor($this->width/$this->codeNum)*$i+5;
  55. $y=rand(0,$this->height-15);
  56. imagechar($this->image,$fontsize,$x,$y,$this->checkCode{$i},$fontcolor);
  57. }
  58. else
  59. {
  60. $fontsize=rand(12,16);
  61. $x=floor(($this->width-8)/$this->codeNum)*$i+8;
  62. $y=rand($fontsize,$this->height-8);
  63. imagettftext($this->image,$fontsize,rand(-45,45),$x,$y,$fontcolor,$fontFace,$this->checkCode{$i});
  64. }
  65. }
  66. }
  67. private function createCheckCode(){
  68. $code="23456789abcdefghijkmnpqrstuvwrst";
  69. $str="";
  70. for($i=0;$i<$this->codeNum;$i++)
  71. {
  72. $char=$code{rand(0,strlen($code)-1)};
  73. $str.=$char;
  74. }
  75. return $str;
  76. }
  77. private function outputImage()
  78. {
  79. if(imagetypes()&IMG_GIF)
  80. {
  81. header("Content-Type:image/gif");
  82. imagepng($this->image);
  83. }else if(imagetypes()&IMG_JPG)
  84. {
  85. header("Content-Type:image/jpeg");
  86. imagepng($this->image);
  87. }else if(imagetypes()&IMG_PNG)
  88. {
  89. header("Content-Type:image/png");
  90. imagepng($this->image);
  91. }else if(imagetypes()&IMG_WBMP){
  92. header("Content-Type:image/vnd.wap.wbmp");
  93. imagepng($this->image);
  94. }else
  95. {
  96. die("PHP不支持图片验证码");
  97. }
  98. }
  99. //通过该方法向浏览器输出图像
  100. function showImage($fontFace="")
  101. {
  102. //创建图像背景
  103. $this->createImage();
  104. //设置干扰元素
  105. $this->setDisturbColor();
  106. //向图像中随机画出文本
  107. $this->outputText($fontFace);
  108. //输出图像
  109. $this->outputImage();
  110. }
  111. function __destruct()
  112. {
  113. imagedestroy($this->image);
  114. }
  115. }
  116. function checklogin(){
  117. if(empty($_POST['name']))
  118. die( 'Username cannot is empty');
  119. if(empty($_POST['password']))
  120. die("Password cannot be empty");
  121. if($_SESSION['code']!=$_POST['vertify'])
  122. die("The verification code input is incorrect".$_SESSION['code']);
  123. $username=$_POST['name'];
  124. $password=md5($_POST['password']);
  125. / /Check if it exists
  126. conndb($username,$password);
  127. }
  128. function conndb($name="",$ps=""){
  129. $conn=mysql_connect('localhost','root','123456' );
  130. if(!$conn) die("Database connection failed".mysql_error());
  131. mysql_select_db('5kan',$conn) or die('Select database failed'.mysql_error());
  132. mysql_set_charset(' utf8',$conn);
  133. $sql="select id from k_user where username='{$name}' and password='{$ps}'";
  134. $result=mysql_query($sql) or die("SQL Statement error".mysql_error());
  135. if(mysql_num_rows($result)>0) die("Login successful");
  136. else die("Username or password is wrong");
  137. mysql_close($conn);
  138. }
  139. session_start();
  140. if(!isset($_POST['randnum']))
  141. {
  142. $code=new ValidationCode(120,20,4);
  143. $code->showImage("comicbd.ttf" ;
  144. Copy code
Verification code, php

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles