The
Authentication code is to prevent the machine from flooding to the website pollution and increase the server burden to appear. At present, large and small sites have verification code. Today, I implemented a simple authentication code class. Simply because there is no interference in the arc and so on, but the text rotated a bit. Of course, because of the font, it's not easy to look at it at a glance. At the same time, in order to avoid the case of letters and digital confusion, but also to remove those who look very much like the alphanumeric.
Class:
<?php/** * Simple to generate Verification code class */class Captcha {private $width;//The Authentication code width private $height;//Verify code height private $countOfChars; Number of characters//private $distrubLines;//number of interference lines private $chars;//randomly generated string public function __construct ($width =100, $height =30, $co
untofchars=4, $distrubLines =2) {//initialization parameters $this->width= $width;
$this->height= $height;
$this->countofchars= $countOfChars;
Session_Start ();
/** * Performs all actions, generates Authenticode and directly outputs/Public function execute () {$imageHandle = $this->createimage ();
$this->createchars ();
$this->drawchars ($imageHandle);
$this->outimage ($imageHandle); /** * Create canvas and randomly fill color * @return return canvas handle/Public Function createimage () {$imageHandle = Imagecreate ($this-&G
T;width, $this->height);
Random background color $randColor =imagecolorallocate ($imageHandle, Mt_rand (0), Mt_rand (0, 50));
Imagefill ($imageHandle, 0, 0, $randColor);
return $imageHandle; /** * generates random characters */private Function Createchars () {//candidate character $sTr= ' ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789 ';
$chars = ';
For ($i =0 $i < $this->countofchars; $i + +) {$chars. = $str [Mt_rand (0,strlen ($STR)-1)];
} $this->chars= $chars;
Save in session $_session[' Captcha ']=strtolower ($chars); /** * Writes characters to image * @param type $imageHandle image handle/Private function Drawchars ($imageHandle) {if ($this->
Chars!=null) {$font = '/home/www/yuweilishuft.ttf '; For ($i =0 $i <strlen ($this->chars), $i + +) {$color = Imagecolorallocate ($imageHandle, Mt_rand (), Mt_rand (
100, 255), 255);
Imagefttext ($imageHandle, $i *20+10,25, $color, $font, $this->chars[$i]); /** * Output image * @param type $imageHandle image handle */Private Function Outimage ($imageHandle) {imagepng (
$imageHandle);
Imagedestroy ($imageHandle); /** * To determine if the user entered the validation code is correct * @param type $usrInput user's input * @return Boolean code matching * * * public static function Isright ($usrInput) {if (Isset ($_session[' Captcha '))) {if (s)Trtolower ($usrInput) ==$_session[' Captcha ']) {$_session[' captcha ']=null;
return true;
}else{$_session[' Captcha ']=null;
return false; }
}
}
}
The validation is set to a static method, because the validation code has been saved to the session after the validation code is generated, and the static method is called directly when validating, without instantiating the class.
The font above can be set randomly.
The following code returns an image that is dynamically generated after instantiating the Captcha class. (outcaptcha.php)
<?php
require (' captcha.php ');
$code = new Captcha ();
Header (' content-type:image/png ');
$code->execute ();
Header (' content-type:image/png ');
The purpose of this sentence is to tell the browser that the PNG image is output, not the HTML code. When the browser is received, the following output is parsed into an image.
Then write an HTML static page (testcaptcha.html) to create the form
Just that is not enough, see the form submitted to the address? That is used to verify that the verification code enters the correct code:
Session_Start ();
$inputCaptcha = Trim ($_post[' input_captcha ']);
Require (' captcha.php ');
if (Captcha::isright ($inputCaptcha)) {
echo ' Verify code is correct ';
} else{
Echo ' authenticode error or expired ';
}
Session_destroy ();
Here you will import the Captcha class and then call its static method to validate your input. Finally, the entire session is destroyed.
Finally, look at the effect.
Great, it's done. Then deliberately lose the wrong try, back up, and then refresh (if not refresh the browser will directly call the cache code picture, this time our verification code has not yet generated!) So whatever it is, it goes wrong.
Of course, the real verification code can be clicked for a single one, which leverages Ajax technology.
The above PHP with session and GD Library to achieve simple verification code generation and verification of the class method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.