PHP Simple Verification Code class: letter + Digital Font random twist _php Tutorial

Source: Internet
Author: User
Tags imagejpeg set background
Now a variety of verification code, personally think that the verification code is for users to see, so simple is good. What interference codes, tilt, complex backgrounds are a torment to the user.
Look at some of the more large experience better site verification code is relatively simple, no complex background, no interference code, no background.
There is no place to write bad welcome everyone to criticize, pointing.
[PHP]
if (!defined (' is_initphp ')) exit (' Access denied! ');
/*********************************************************************************
* initphp 2.0 domestic PHP Development Framework Extension Class Library-verification code
*-------------------------------------------------------------------------------
* All rights reserved: Copyright by initphp.com
* You are free to use the source code, but in the course of use, please keep the author information. Respect for the fruits of others ' work is respect for themselves
*-------------------------------------------------------------------------------
* $Author: liuxinming
* $Dtime: 2012-10-09
***********************************************************************************/
Class seccodeinit{
Private $width;
Private $height;
Private $type =0;//0 Letters + Digital Verification Code
Private $time =3000;//Verification code expiry time (s)
Private $color =null;//captcha font Color
Private $im;
Private $length =4;//Verification code length
private $warping;//Random Twist

/**
* Get random values
* @return String
*/
Private Function Get_random_val () {
$i = 0;
while ($i < $this->length)
{
Mt_srand (Double) microtime () *1000000);
$randnum =mt_rand (50,90);
if (!in_array ($randnum, Array (58,59,60,61,62,63,64,73,79)))
{
$authnum = $authnum. chr ($randnum);
$i + +;
}
}
Session_Start ();
$time =time ();
$checkcode =MD5 (MD5 ($authnum. ' Initphpyzmsy '. $time));
$key = $time. ', '. $checkcode. ', '. Authnum;
$_session[' initphp_code '] = $key;
return $authnum;
}

/**
* Get Verification code picture
* @param $width Wide
* @param $height High
* @param $warping Font random twist switch 0 = off, 1 = on
* @return String
*/
Public Function GetCode ($width =140, $height =40, $warping =0) {
$this->width= $width;
$this->height= $height;
$this->warping= $warping;
if ($this->type<2&& function_exists (' imagecreate ') && function_exists (' Imagecolorset ') & & Function_exists (' imagecopyresized ') && function_exists (' imagecolorallocate ') && Function_ Exists (' Imagechar ') && function_exists (' Imagecolorsforindex ') &&
Function_exists (' Imageline ') && function_exists (' imagecreatefromstring ') && (function_exists (' Imagegif ') | | Function_exists (' imagepng ') | | Function_exists (' imagejpeg ')) {
$this->image ();
}
}
/**
* Generate Image Verification code
* @return String
*/
Public function image () {
$this->im=imagecreate ($this->width, $this->height);//Set Picture background size
Imagecolorallocate ($this->im, 243, 251, 254);//Set background
$this->color=imagecolorallocate ($this->im, Mt_rand (1,120), Mt_rand (1,120), Mt_rand (1,120));//captcha font random color
$ttfPath = DirName (__file__). '/font/';//font Directory
$dirs = Opendir ($ttfPath);
$seccodettf = Array ();
while ($entry = Readdir ($dirs)) {
if ($entry! = '. ' && $entry! = ': ' && in_array (addslashes (Strtolower (substr (STRRCHR ($entry, '. '), 1))), Array (' TTF ', ' TTC '))) {

$seccodettf [] = $ttfPath. $entry;
}
}
$ttf = $seccodettf [Array_rand ($seccodettf)];//a random font
$size = $this->type? $this-&GT;WIDTH/7: $this->width/6;//Font Size
Imagettftext ($this->im, $size, 0, ten, $size *1.2, $this->color, $ttf, $this->get_random_val ());//Set Authenticode characters
if ($this->warping) {//Random twist
$this->setwarping ();
}
if (function_exists ("Imagepng"))
{
Header ("Content-type:image/png");
$code =imagepng ($this->im);
}elseif (function_exists ("imagejpeg"))
{
Header ("Content-type:image/jpeg");
$code =imagejpeg ($this->im);
}elseif (function_exists ("Imagegif"))
{
Header ("Content-type:image/gif");
$code =imagegif ($this->im);
}
Imagedestroy ($this->im);
return $code;
}

/**
* Check the verification code
* @param $code
* @return BOOL
*/
Public Function Checkcode ($code) {
Session_Start ();
$secode =explode (', ', $_session[' initphp_code ');
$time =time ();
Check if time expires
if ($secode [0]> $time | | $time-$secode [0]> $this->time)
{
return false;
}
Verify code key is consistent after double MD5
if ($secode [1]&LT;&GT;MD5 (MD5 ($code. ' Initphpyzmsy '. $secode [0]))) {
return false;
}
Check that the verification code string is consistent
if ($code | | $code <> $secode [2])
{
return false;
}
return true;
}

/* Random twist */
Public Function setwarping () {
$rgb =array ();
$direct =rand (0,1);
$width = Imagesx ($this->im);
$height = Imagesy ($this->im);
$level = $width/20;
for ($j = 0; $j < $height; $j + +) {
for ($i = 0; $i < $width; $i + +) {
$rgb [$i] = Imagecolorat ($this->im, $i, $j);
}
for ($i = 0; $i < $width; $i + +) {
$r = sin ($j/$height * 2 * M_PI-M_PI * 0.5) * ($direct? $level:-$level);
Imagesetpixel ($this->im, $i + $r, $j, $rgb [$i]);
}
}
}
}
?>
Effect:

http://www.bkjia.com/PHPjc/477945.html www.bkjia.com true http://www.bkjia.com/PHPjc/477945.html techarticle now a variety of verification code, personally think that the verification code is for users to see, so simple is good. What interference codes, tilt, complex backgrounds are a torment to the user. Have a look ...

  • 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.