PHP development user login module to create a simple verification code

The function of the verification code:

Effectively prevent a hacker from making continuous login attempts on a specific registered user using a specific program to brute force. In fact, modern verification codes generally prevent machines from registering in batches and preventing machines from posting replies in batches. Currently, many websites use verification code technology to prevent users from using robots to automatically register, log in, and spam.


The so-called verification code is to generate a picture from a series of randomly generated numbers or symbols, with some interfering pixels added to the picture (to prevent OCR), the user visually recognizes the verification code information, enters the form and submits it for website verification. Only after successful verification can a certain function be used.


Our most common verification code
1, four digits, a random one-digit string, the most original verification code, the verification effect is almost zero.
2, random digital picture verification code. The characters on the picture are quite regular, some may have some random interferon added, and some may have random character colors, so the verification effect is better than the previous one. People without basic knowledge of graphics and imagery cannot break it!
3, random numbers in various picture formats + random uppercase English letters + random interference pixels + random positions.
4. Chinese characters are the latest verification codes for registration. They are randomly generated, making it more difficult to type and affecting the user experience. Therefore, they are generally less commonly used.

For the sake of simplicity, the main object of our explanation this time is the second type. Let’s first look at several pictures of this kind of verification code that are more common on the Internet.
These four styles can basically Representing the types of verification codes mentioned in 2, initially it seems that the first picture is the easiest to crack, the second is the second, the third is more difficult, and the fourth is the most difficult.
What’s the real situation? In fact, these three types of images are equally difficult to crack.
The first picture is the easiest. The background and numbers of the picture use the same color, the characters are regular and the characters are in the same position.
The second picture seems not easy. In fact, if you study it carefully, you will find its rules. No matter how the background color and interferon change, the verification characters are regular and the same color, so it is very easy to eliminate interferon, as long as they are all non-character pigments. Just exclude it.
The third picture seems to be more complicated. In addition to the background color and interferon changing as mentioned above, the color of the verification characters also changes, and the colors of each character are also different. It seems impossible to break through this verification code. This article will take this type of verification code as an example. In the fourth picture, students can create it themselves.
In the fourth picture, in addition to the features mentioned in the third picture, two straight lines of interference rate are added to the text. It seems difficult but is actually easy to remove.

Publish a basic image that generates a png image verification code:

1. Generate a png picture
2. Set the background color for the picture
3. Set the font color and style
4. Generate a 4-digit random verification code
5. Adjust the rotation angle and position of each generated character and draw it on the png image
6. Add noise and interference lines to prevent the registration machine from analyzing the original image for malicious registration
7. Output the image
8. Release the image Memory occupied

The following is an example of the verification code code:

<?php
//设置session,必须处于脚本最顶部
  session_start();
  
  $image = imagecreatetruecolor(100, 30);    //1>设置验证码图片大小的函数
  //5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue);
  $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
  //6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色
  imagefill($image, 0, 0, $bgcolor);
  //10>设置变量
  $captcha_code = "";
  //7>生成随机数字
  for($i=0;$i<4;$i++){
    //设置字体大小
    $fontsize = 6;
    //设置字体颜色,随机颜色
    $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120));      //0-120深颜色
    //设置数字
    $fontcontent = rand(0,9);
    //10>.=连续定义变量
    $captcha_code .= $fontcontent;
    //设置坐标
    $x = ($i*100/4)+rand(5,10);
    $y = rand(5,10);
   imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
  }
  //10>存到session
  $_SESSION['authcode'] = $captcha_code;
  //8>增加干扰元素,设置雪花点
  for($i=0;$i<200;$i++){
    //设置点的颜色,50-200颜色比数字浅,不干扰阅读
    $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
    //imagesetpixel — 画一个单一像素
    imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
  }
  //9>增加干扰元素,设置横线
  for($i=0;$i<4;$i++){
    //设置线的颜色
    $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
    //设置线,两点一线
    imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
  }
  //2>设置头部,image/png
  header('Content-Type: image/png');
  //3>imagepng() 建立png图形函数
  imagepng($image);
  //4>imagedestroy() 结束图形函数 销毁$image
  imagedestroy($image);
?>

The display style is as follows:

y3.png

Continuing Learning
||
<?php //设置session,必须处于脚本最顶部 session_start(); $image = imagecreatetruecolor(100, 30); //1>设置验证码图片大小的函数 //5>设置验证码颜色 imagecolorallocate(int im, int red, int green, int blue); $bgcolor = imagecolorallocate($image,255,255,255); //#ffffff //6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着色,col 表示欲涂上的颜色 imagefill($image, 0, 0, $bgcolor); //10>设置变量 $captcha_code = ""; //7>生成随机数字 for($i=0;$i<4;$i++){ //设置字体大小 $fontsize = 6; //设置字体颜色,随机颜色 $fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜色 //设置数字 $fontcontent = rand(0,9); //10>.=连续定义变量 $captcha_code .= $fontcontent; //设置坐标 $x = ($i*100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } //10>存到session $_SESSION['authcode'] = $captcha_code; //8>增加干扰元素,设置雪花点 for($i=0;$i<200;$i++){ //设置点的颜色,50-200颜色比数字浅,不干扰阅读 $pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 画一个单一像素 imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } //9>增加干扰元素,设置横线 for($i=0;$i<4;$i++){ //设置线的颜色 $linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220)); //设置线,两点一线 imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor); } //2>设置头部,image/png header('Content-Type: image/png'); //3>imagepng() 建立png图形函数 imagepng($image); //4>imagedestroy() 结束图形函数 销毁$image imagedestroy($image); ?>
submitReset Code