How to implement code generator in PHP

Source: Internet
Author: User
Tags php code generator
This article is mainly for you to introduce the PHP code generator related codes, with a certain reference value, interested in small partners can refer to

Many Web sites now have user sets implemented. However in order to prevent the network attack of the robot. It is necessary to restrict login or registration.
It is a good choice to force a set of strings that are difficult to recognize when registering and logging in. While the underlying problem cannot be solved, it can at least increase their cost.

The GD2 library is used to generate the verification code using PHP. GD2 Library Reference methods There are many networks, and different operating systems are imported in different ways.

This code runs on the WinDOS server platform

<?php$ic = new Idcode (5,60,30), $iC->createpng (), class idcode{Private $words = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ' , ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' h ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 '  );  Private $fonts;  private $count;//The number of authentication code characters private $height;  Private $width; Private $path = '..  \myfolder\fonts ';  Private $keys;    Constructor public Function __construct ($count, $width, $height) {$this->count = $count;    $this->getfonts ();    $this->height = $height;  $this->width = $width;    } Private Function Getfonts () {$dir = Dir ($this->path); while (false!== ($file = $dir->read ())) {if ($file! = '. ' && $file! = ')        {$this->fonts[count ($this->fonts)] = basename ($file);  }} $dir->close (); } Private Function Createkeys () {for ($i = 0; $i < $this->count; $i ++) {$this->keys[$i] [' char '] = $this->words[rand (0,count ($this->words)-1)]; Use the font path to identify $this->keys[$i [' filename '] = $this->path. '    \ \ '. $this->fonts[rand (0,count ($this->fonts)-1)];    }} Public Function Createpng () {$this->createkeys (); Create canvas and color blocks $BG = Imagecreatetruecolor ($this->width + 10*2, $this->height + 3*2);//left 10px blank on both sides, up and down 3px $grey = ima    Gecolorallocate ($BG, 155,155,155);    $blue = Imagecolorallocate ($BG, 0x00,0x00,0xff);    Fill Background Imagefill ($BG, 0,0, $grey);    Add character $pwidth = $this->width/$this->count;    $x; $y;      for ($i = 0; $i < $this->count; $i + +) {$rotation = rand ( -40,40);//deflection angle ±40° $fontsize = 33;      $width _txt;      $height _txt;        do{$fontsize--;        $bbox = Imagettfbbox ($fontsize, $rotation, $this->keys[$i [' filename '], $this->keys[$i] [' char ']);      $width _txt = $bbox [2]-$bbox [0];//x 0 2 4 6,y1 3 5 7; lower left, lower right, upper right, upper left $height _txt = $bbox [7]-$bbox [1]; }while ($fontsize > 8 && ($height _txt > $this->height | | $width _txt > $pwidth));      $fontcolor = Imagecolorallocate ($BG, Rand (0,255), Rand (0,255), Rand (0,255));      $x = 8 + $pwidth * $i + $pwidth/2-$width _txt/2;//x coordinates basic position $y = $this-&GT;HEIGHT/2-$height _TXT/2; Imagettftext ($BG, $fontsize, $rotation, $x, $y, $fontcolor, $this->keys[$i] [' filename '], $this->keys[$i] [' char '    ]);    }//Draw interference lines//Add interference line Imageline ($BG, 0,15,40,10, $blue) as appropriate;    Image Output header file header (' Content-type:image/png ');    Output PNG image imagepng ($BG);  Clear Cache Resource Imagedestroy ($BG);    The Public Function Checkkeys ($input) {if (count ($input)! = $this->count) {return ' ERROR: incorrect length. '; }else{for ($i =0; $i < $this->count; $i +) {//0 o i l 1 calibration, determine if manual calibration is required based on the selected font ($input [$i]! = $th        is->keys[$i] [' char ']) {return ' SUCCESS. ';        }else{return ' ERROR: Please enter the correct verification code. '; }}}}}?>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.