How to implement Authentication Code Class with php
This article is to share with you about how php implements the authentication code class. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Go directly to the code:
/ / Verification code class
Class ValidateCode {
Private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';// random factor
Private $code;// CAPTCHA
Private $codelen = 4 Band / CAPTCHA length
Private $width = 130bp / width
Private $height = 50 Splink / height
Private $img;// graphics resource handle
Font specified by private $font;//
Private $fontsize = 20position / specified font size
Private $fontcolor;// specifies the font color
/ / Constructor initialization
Public function _ construct () {
$this- > font = dirname (_ _ FILE__).'/ font/elephant.ttf';// note that the font path must be written correctly, otherwise the picture will not be displayed
}
/ / generate random codes
Private function createCode () {
$_ len = strlen ($this- > charset)-1
For ($iposit / iTunes / icodeln +) {
$this- > code. = $this- > charset [Mt _ rand
}
}
/ / generate background
Private function createBg () {
$this- > img = imagecreatetruecolor ($this- > width, $this- > height)
$color = imagecolorallocate ($this- > img, mt_rand (157255), mt_rand (157255), mt_rand (157255))
Imagefilledrectangle ($this- > img,0,$this- > height,$this- > width,0,$color)
}
/ / generate text
Private function createFont () {
$_ x = $this- > width / $this- > codelen
For ($iposit / iTunes / icodeln +) {
$this- > fontcolor = imagecolorallocate ($this- > img,mt_rand (0156), mt_rand (0156), mt_rand (0156))
Imagettftext ($this- > img,$this- > fontsize,mt_rand (- 30 x*$i+mt_rand 30), $this- > height / 1.4 font,$this- > code [$I])
}
}
/ / generate lines and snowflakes
Private function createLine () {
/ / Line
For ($itimatic, mt_rand (0156), mt_rand (0156))
Imageline ($this- > img,mt_rand (0penny-> width), mt_rand (0meme-> height), mt_rand (0meme-> width), mt_rand (0meme-> height), $color)
}
/ / Snowflake
For ($itimatic, mt_rand (200255), mt_rand (200255))
Imagestring ($this- > img,mt_rand (1jime 5), mt_rand (0djinthis-> width), mt_rand (0recorder-> height),'*', $color)
}
}
/ / output
Private function outPut () {
Header ('Content-type:image/png')
Imagepng ($this- > img)
Imagedestroy ($this- > img)
}
/ / external generation
Public function doimg () {
$this- > createBg ()
$this- > createCode ()
$this- > createLine ()
$this- > createFont ()
$this- > outPut ()
}
/ / obtain the verification code
Public function getCode () {
Return strtolower ($this- > code)
}
}
Output example:
How to use it:
1. Save the CAPTCHA class to a file named ValidateCode.class.php
2. Create a new file named captcha.php to call the class
Captcha.php
The copy code is as follows:
Session_start ()
Require'. / ValidateCode.class.php'; / / include the class first, and the actual path will be modified according to the actual situation.
$_ vc = new ValidateCode (); / / instantiate an object
$_ vc- > doimg ()
$_ SESSION ['authnum_session'] = $_ vc- > getCode (); / / the CAPTCHA is saved to SESSION
3. Reference to the page, the code is as follows:
The copy code is as follows: