Home Backend Development PHP Tutorial Verification code PHP class supports numbers, letters, Chinese characters, mixed

Verification code PHP class supports numbers, letters, Chinese characters, mixed

Jul 25, 2016 am 08:46 AM

  1. // +---------------------------------------- ----------------------------------
  2. // Verification code class, objects of this class can dynamically obtain verification codes Pictures and verification code characters are saved in SESSION['code']
  3. // +-------------------------------- ----------------------------------------
  4. //Supports 4 formats of numbers, letters and Chinese characters Mix
  5. // +------------------------------------------------ --------------------------
  6. // @Author: HelloChina(sanzi0930@163.com)
  7. // +----- -------------------------------------------------- ------------------
  8. // @Date: June 7, 2012 11:03:00
  9. // +------------ -------------------------------------------------- ----------
  10. // @version 1.0
  11. // +-------------------------------- ------------------------------------------
  12. class Vcode{
  13. protected $ width; //Verification code width
  14. protected $height; //Verification code length
  15. protected $codeNum; //Verification code number of characters
  16. protected $codeType; //Verification code type
  17. protected $fontSize; //Character size
  18. protected $fontType; //Font type
  19. protected $codeStr; //Chinese content
  20. protected $strNum; //Chinese number
  21. protected $imageType; //Output image type
  22. protected $image; //Image resource
  23. protected $checkCode; //Verification code content
  24. /**
  25. +------------------------------------------------ --------------------------------
  26. * Get verification code information
  27. +---------- -------------------------------------------------- --------------------
  28. * @param integer $width Verification code width
  29. * @param integer $height Verification code height
  30. * @param integer $codeNum Verification code character Number
  31. * @param integer $codeType Verification code character type 1 is number 2 is letter 3 is Chinese character 4 is mixed
  32. * @param integer $fontSize Verification code font size
  33. * @param string $fontType Verification code font type
  34. * @param string $imageType Verification code output image type
  35. * @param string $codestr Chinese verification code content
  36. +-------------------------- -------------------------------------------------- ----
  37. */
  38. public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti. ttf' ,$imageType='jpeg', $codeStr='Fuck me, it's flat. Yes, it flies well. Just think about it.'){
  39. $this->width = $width ;
  40. $this->height = $height;
  41. $this->codeNum = $codeNum;
  42. $this->codeType = $codeType;
  43. $this->fontSize = $fontSize;
  44. $this-> ;fontType = $fontType;
  45. $this->codeStr = $codeStr;
  46. $this->strNum = strlen($this->codeStr)/3-1;
  47. $this->imageType = $imageType;
  48. $this->checkCode = $this->getCheckCode();
  49. }
  50. //+-------------------------- -------------------------------------------------- ----
  51. //* Generate verification code characters
  52. //+---------------------------------- --------------------------------------------------
  53. //* @return string
  54. //+---------------------------------------------- ----------------------------------------
  55. public function __toString(){
  56. $string = implode( '', $this->getCheckCode());
  57. $_SESSION["code"]=$string; //Add to session
  58. $this->getImage(); //Output verification code
  59. return '' ;
  60. }
  61. protected function getCheckCode(){
  62. $string = array();
  63. switch($this->codeType){
  64. case 1:
  65. //numeric string
  66. $string = array_rand(range(0,9 ), $this->codeNum);
  67. break;
  68. case 2:
  69. //Large letter string
  70. $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum );
  71. break;
  72. case 3:
  73. //Chinese character string
  74. for($i=0; $i<($this->codeNum); $i++){
  75. $start = mt_rand(0, $this ->strNum);
  76. $string[$i]= self::msubstr($this->codeStr,$start);
  77. }
  78. break;
  79. case 4:
  80. //Mixed strings
  81. for($i=0; $i<($this->codeNum); $i++){
  82. $rand=mt_rand(0,2);
  83. switch ($rand){
  84. case 0:
  85. $ascii = mt_rand(48,57);
  86. $string[$i] = sprintf('%c',$ascii);
  87. break;
Copy code

Verification code, PHP
This topic was moved by Xiaobei on 2015-11-18 08:23


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)

Stremio subtitles not working; error loading subtitles Stremio subtitles not working; error loading subtitles Feb 24, 2024 am 09:50 AM

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

C++ program: rearrange the position of words in alphabetical order C++ program: rearrange the position of words in alphabetical order Sep 01, 2023 pm 11:37 PM

In this problem, a string is given as input and we have to sort the words appearing in the string in lexicographic order. To do this, we assign an index starting from 1 to each word in the string (separated by spaces) and get the output in the form of sorted indices. String={"Hello","World"}"Hello"=1 "World"=2 Since the words in the input string are in lexicographic order, the output will print "12". Let's look at some input/result scenarios - Assuming all words in the input string are the same, let's look at the results - Input:{"hello","hello","hello"}Result:3 Result obtained

How to use Golang to determine whether a character is a letter How to use Golang to determine whether a character is a letter Dec 23, 2023 pm 04:09 PM

Golang implementation: Methods to determine whether a character is a letter In Golang, there are many ways to determine whether a character is a letter. This article will introduce several of these commonly used methods and provide specific code examples for each method. Method 1: Use the IsLetter function of the Unicode package. The Unicode package in Golang provides a function called IsLetter, which can determine whether a character is a letter. This function is used as follows: packagemaini

The internal code of a Chinese character requires several bytes to store The internal code of a Chinese character requires several bytes to store Dec 14, 2020 pm 05:45 PM

The internal code of a Chinese character requires 2 bytes to store. In the popular Chinese character system in China, the internal code of a Chinese character occupies 2 bytes. Because the Chinese character processing system must ensure compatibility between Chinese and Western languages, ambiguity will occur when ASCII codes and Chinese character national standard codes exist in the system. ; To this end, the Chinese character internal code should be appropriately processed and transformed into the national standard code.

How to use Golang to determine whether a character is a letter How to use Golang to determine whether a character is a letter Dec 23, 2023 am 11:57 AM

How to use Golang to determine whether a character is a letter. In Golang, determining whether a character is a letter can be achieved by using the IsLetter function in the Unicode package. The IsLetter function checks whether the given character is a letter. Next, we will introduce in detail how to use Golang to write code to determine whether a character is a letter. First, you need to create a new Go file in which to write the code. You can name the file "main.go". code

Master the skills of PHP processing Chinese character transcoding Master the skills of PHP processing Chinese character transcoding Mar 28, 2024 pm 03:47 PM

PHP is a widely used server-side scripting language commonly used for website development. During website development, we often encounter the need to transcode Chinese characters, especially when dealing with Chinese characters. Mastering the skills of PHP in processing Chinese character transcoding can effectively avoid problems such as garbled characters and improve the stability and user experience of the website. 1.utf8_encode and utf8_decode functions In PHP, you can use the utf8_encode and utf8_decode functions to encode and decode Chinese characters.

In-depth understanding of the principle of converting Chinese characters to UTF-8 encoding in PHP In-depth understanding of the principle of converting Chinese characters to UTF-8 encoding in PHP Mar 28, 2024 pm 02:44 PM

The principle of converting Chinese characters to UTF-8 encoding actually involves the concept of character encoding. In computers, text characters need to be represented and stored in the form of numbers, and different character encoding schemes specify the correspondence between different characters and numbers. UTF-8 is a commonly used character encoding method. It supports characters worldwide and uses a variable-length encoding method, which can effectively represent characters in various languages ​​and is especially suitable for the Unicode character set. As a common server-side scripting language, PHP also provides

Solution to Chinese character input problem in win11 Solution to Chinese character input problem in win11 Jan 05, 2024 am 08:29 AM

After we install the win11 system, we must first install the win11 Chinese input method to type Chinese characters. If after installing the Chinese input method, we still cannot type Chinese characters, then the related service may be disabled. Just restart it. The following is Get up and take a look. What to do if win11 cannot type Chinese characters: 1. First, we must make sure that we have downloaded and installed the Chinese input method or third-party input method software. 2. If you don’t know how to add an input method, you can check out the tutorials on this site. 3. If you still cannot type Chinese characters after adding the input method, you need to enable related services. 4. First, right-click the start menu and find "Computer Management" 5. Then enter "Task Scheduler" - "Task Scheduler"

See all articles