A simple and safe PHP authentication code class, PHP verification code _php Instance

Source: Internet
Author: User
Tags asin set background sin

One, sample verification code

Second, PHP Authentication code class, secoder.class.php

<?php/** * Security Verification Code to: Verify code text distort, rotate, use different fonts, add interference code * * @author Running water Mengchun <cmpan (at) qq.com> * @link http://l Abs.yulans.cn/yl_security_secoder * @link Http://wiki.yulans.cn/docs/yl/security/secoder * * Class Yl_security_ 
Secoder {/** * Verification Code of the session of the subscript * * @var string///public static $seKey = ' Sid.sek ey.ylans.cn '; 
public static $seKey = ' Sid '; public static $expire = 3000; Authenticode expiration (s)/** * code used in the character, 01IO easy to confuse, recommended not * * @var string/public static $codeSet = ' 346789ABCDEFGHJKLMNPQRTUVWX 
Y '; public static $fontSize = 25; Authenticode font size (px) public static $useCurve = true; Whether to draw a confusing curve public static $useNoise = true; Add pragma public static $imageH = 0; Verify code picture wide public static $imageL = 0; Verify code picture long public static $length = 4; Verify the number of code digits public static $BG = Array (243, 251, 254); Background protected static $_image = NULL; Verification code picture instance protected static $_color = NULL; Verify code Font Color/** * Output Verification code and save the value of the validation code in the session * Verify code saved to the session format: $_session[self:: $seKey] = array (' COde ' => ' Verification Code value ', ' time ' => ' verification Code created ');  */public static function entry () {//Picture width (px) Self:: $imageL | | self:: $imageL = self:: $length * Self:: $fontSize * 1.5 + 
Self:: $fontSize *1.5; Picture High (px) Self:: $imageH | | 
Self:: $imageH = self:: $fontSize * 2; 
Establish a self:: $imageL x Self:: $imageH image self::$_image = Imagecreate (self:: $imageL, Self:: $imageH); 
Set Background imagecolorallocate (Self::$_image, Self:: $BG [0], Self:: $BG [1], Self:: $BG [2]); 
CAPTCHA font random Color Self::$_color = imagecolorallocate (Self::$_image, Mt_rand (1,120), Mt_rand (1,120), Mt_rand (1,120)); The validation code uses a random font//$ttf = DirName (__file__). '/ttfs/'. Mt_rand (1, 20). '. TTF '; 4 $ttf = dirname (__file__). 
'/ttfs/4.ttf '; 
if (self:: $useNoise) {//Paint Miscellaneous point self::_writenoise (); 
} if (self:: $useCurve) {//Draw interference line self::_writecurve (); //Draw Verification Code $code = Array (); Verification code $codeNX = 0; 
The left margin for the nth character of the authentication code for ($i = 0; $i <self:: $length; $i + +) {$code [$i] = self:: $codeSet [Mt_rand (0, 27)]; $codeNX + + Mt_rand (self:: $fontSize *1.2, self::$fontsize*1.6); Write a captcha character imagettftext (Self::$_image, Self:: $fontSize, Mt_rand ( -40), $codeNX, self:: $fontSize *1.5, Self::$_color 
, $ttf, $code [$i]); }//Save Verification Code isset ($_session) | | 
Session_Start (); $_session[self:: $seKey] [' code '] = Join (", $code); Save the check code to session $_session[self:: $seKey [' time '] = time (); 
Verify Code creation Time Header (' Cache-control:private, max-age=0, No-store, No-cache, Must-revalidate '); 
Header (' cache-control:post-check=0, pre-check=0 ', false); 
Header (' Pragma:no-cache '); 
Header ("Content-type:image/png"); 
Output Image imagepng (self::$_image); 
Imagedestroy (Self::$_image); /** * Draw a random sine function curve that is composed of two lines (you can change to a more handsome curve function) * * The mathematical formula of the high school I forgot to go to Nirvana, write to * sine function analytic formula: Y=asin (ωx+φ) +b * The effect of each constant value on the function image: * A: Determine peak (i.e. multiple of longitudinal stretch compression) * B: Indicates the position of the waveform in the Y axis or the longitudinal distance (up and down) *φ: Determines the waveform and the position of the x axis or the transverse moving distance (left plus right minus) *ω: Determining the period (minimum positive period t=2π/∣ω∣) * * * * */protected S tatic function _writecurve () {$A = Mt_rand (1, Self:: $imageH/2);//Amplitude $b = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); Y-axis Offset $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X axis Offset $T = Mt_rand (self:: $imageH *1.5, Self:: $imageL *2); 
Cycle $w = (2* m_pi)/$T; $px 1 = 0; The starting position of the curve axis $px 2 = Mt_rand (self:: $imageL/2, Self:: $imageL * 0.667); End position of curvilinear axis for ($px = $px 1; $px <= $px 2; $px = $px + 0.9) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + self:: $imageH/ 2; 
y = Asin (ωx+φ) + b $i = (int) (self:: $fontSize-6)/4); while ($i > 0) {imagesetpixel (self::$_image, $px + $i, $py + $i, self::$_color);//Here The portrait is more than Imagettftext and imagestring performance. 
Much better $i--; }} $A = Mt_rand (1, Self:: $imageH/2); Amplitude $f = Mt_rand (-self:: $imageH/4, Self:: $imageH/4); X axis Offset $T = Mt_rand (self:: $imageH *1.5, Self:: $imageL *2); 
Cycle $w = (2* m_pi)/$T; 
$b = $py-$A * sin ($w * $px + $f)-Self:: $imageH/2; 
$PX 1 = $px 2; 
$px 2 = self:: $imageL; for ($px = $px 1; $px <= $px 2; $px = $px + 0.9) {if ($w!=0) {$py = $A * Sin ($w * $px + $f) + $b + self:: $imageH/2;//y = Asin 
(ωx+φ) + b $i = (int) (self:: $fontSize-8)/4); while ($i > 0) {imagesEtpixel (self::$_image, $px + $i, $py + $i, self::$_color); 
Here (while) the loop portrait element is much better than the Imagettftext and imagestring using the font size at once (without this while loop) $i--; 
/** * Draw Miscellaneous points * Write different color letters or numbers to the picture * * protected static function _writenoise () {for ($i = 0; $i < $i + +) { 
Miscellaneous point Color $noiseColor = Imagecolorallocate (Self::$_image, Mt_rand (150,225), Mt_rand (150,225), Mt_rand (150,225)); for ($j = 0; $j < 5; $j + +) {//Paint Miscellaneous point imagestring (Self::$_image, 5, Mt_rand ( -10, Self:: $imageL), Mt_rand ( -10, self: 
: $imageH), Self:: $codeSet [Mt_rand (0, 27)],//Miscellaneous point text is a random letter or number $noiseColor); /** * Verify that the verification code is correct * * @param string $code user authentication code * @param BOOL user authentication code is correct */public static function check ($code 
) {isset ($_session) | | | session_start (); Authenticode cannot be empty if (empty ($code) | | empty ($_session[self:: $seKey]) {//echo $_session[self:: $seKey [' Code ']. ' 
1 '; 
return false; 
//Session expiration if (time ()-$_session[self:: $seKey] [' time '] > Self:: $expire) {unset ($_session[self:: $seKey]); Echo $_seSsion[self:: $seKey] [' Code ']. ' 
2 '; 
return false; 
return 0; }//if ($code = = $_session[self:: $seKey [' Code ']) {if strtoupper ($code) = = $_session[self:: $seKey [' Code ']] {//case-insensitive More//echo $_session[self:: $seKey [' Code ']. ' 
3 '; 
return true; }//echo $_session[self:: $seKey [' Code ']. ' 
4 '; 
return false; }//useage/* Yl_security_secoder:: $useNoise = false; 
To be more secure, change to True yl_security_secoder:: $useCurve = true; 
Yl_security_secoder::entry (); */*//Verify code if (! 
Yl_security_secoder::check (@$_post[' Secode ')) {print ' Error secode '; } 
*/

Third, call the method

1, display the Authentication code page code.php

<?php 
session_start (); 
Require ' secoder.class.php '; First the class is included, the actual path is modified according to the actual situation. 
$vcode = new Yl_security_secoder ();//Instantiate an object 
$vcode->entry (); 
? >

2, check the verification code is correct

<?php 
session_start (); 
Require ' secoder.class.php '; First the class is included, the actual path is modified according to the actual situation. 
$vcode = new Yl_security_secoder ();//Instantiate an object 
//$vcode->entry (); 
$code = $_get[' code ']; 
echo $vcode->check ($code); 
$_session[' Code ' = $VC->getcode ()//Verify code is saved to session 
?>

3, Verification Code input Box call page

 click the picture to retrieve the verification code <br> 
<a href= "# ">

The above is a small set to introduce a simple and safe PHP code class, PHP verification code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.