Example Analysis of php Authentication Code Class
This article mainly introduces the php verification code class example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The details are as follows
/ class ValidateCode {private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';// Random Factor private $code;// CAPTCHA private $codelen = 4 width / CAPTCHA length private $width = 130 width / width private $height = 50 width / height private $graphics resource handle private $font;// specified font private $fontsize = 20 Landmark / specified font size private $fontcolor / / specify font color / / Constructor initializes public function _ _ construct () {$this- > font = dirname (_ _ FILE__).'/ font/elephant.ttf';// Note that the font path must be written correctly, otherwise the random code private function createCode () {$_ len = strlen ($this- > charset)-1; for ($this- > code.) {$this- > code. = $this- > charset [mt _ rand] will not be displayed. }} / / 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 ) {$this- > fontcolor = imagecolorallocate ($this- > img,mt_rand (0156), mt_rand (0156), mt_rand (0156)); imagettftext ($this- > img,$this- > fontsize,mt_rand (- 30 fontsize,mt_rand), $_ x*$i+mt_rand (1 x*$i+mt_rand 5), $this- > height / 1.4 x*$i+mt_rand-> fontcolor,$this- > font,$this- > code [$I]);}} / generate lines, snowflakes private function createLine () {/ / Line for ($item0) $iimg,mt_rand (0156), 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 ($itemisand (200255), mt_rand (200255), mt_rand (200255)) Imagestring ($this- > img,mt_rand (1Power5), mt_rand (0scorethis-> width), mt_rand (0Graysis-> height),'*', $color);}} / output private function outPut () {header ('Content-type:image/png'); imagepng ($this- > img); imagedestroy ($this- > img);} / externally generated public function doimg () {$this- > createBg (); $this- > createCode (); $this- > createLine (); $this- > createFont (); $this- > outPut () } / / get 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
Session_start (); require'. / ValidateCode.class.php'; / / include the class first, and the actual path is modified according to the actual situation. $_ vc = new ValidateCode (); / / instantiate an object $_ vc- > doimg (); $_ SESSION ['authnum_session'] = $_ vc- > getCode (); / / Save the CAPTCHA to SESSION
3. Reference to the page, the code is as follows:
The copy code is as follows: