PHP graphic verification code sharing, php graphic Verification Code

Source: Internet
Author: User

PHP graphic verification code sharing, php graphic Verification Code

Effect:

Myvcode. class. php: class that encapsulates the verification code

<? Php
/*
* File: myvcode. class. php
* Verification Code Class, Class Name Vcode
*/
Class Vcode
{
Private $ width;/* Verification Code width */
Private $ height;/* Verification Code height */
Private $ codeNum;/* number of characters in the Verification Code */
Private $ checkCode;/* Verification Code character */
Private $ image;/* Verification Code resource */
Private $ pixNum;/* Number of interference points to be drawn */
Private $ lineNum;/* number of lines that draw interference lines */
/*
* Constructor instantiate the verification code object and initialize data
* @ Param int $ width: Set the default width.
* @ Param int $ height: sets the default height.
* @ Param int $ codeNum: set the number of characters in the verification code.
* @ Param int $ pixNum: sets the number of interference points.
* @ Param int $ lineNum: sets the number of interference lines.
*/
Function _ construct ($ width = 80, $ height = 40, $ codeNum = 4, $ pixNum = 40, $ lineNum = 5)
{
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> pixNum = $ pixNum;
$ This-> lineNum = $ lineNum;
}
/* Internal private method to create image resources */
Private function getCreateImage ()
{
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
$ White = imagecolorallocate ($ this-> image, 0xff, 0xff, 0xff );
Imagefill ($ this-> image, 0, 0, $ white );
$ Black = imagecolorallocate ($ this-> image, 0, 0 );
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ black );
}
/* Internal private method, draw characters, remove o0Llz and 012 */
Private function createCheckCode ()
{
$ Code = '3456789abcdefghijkmnpqrstuvwxyabcdefghijkmnpqrstuvwxy ';
$ This-> checkCode = "";
For ($ I = 0; $ I <$ this-> codeNum; $ I ++)
{
$ Char = $ code {rand (0, strlen ($ code)-1 )};
$ This-> checkCode. = $ char;
$ FontColor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 ));
$ FontSize = rand (3, 5 );
$ X = rand (0, $ this-> width-imagefontwidth ($ fontSize ));
$ Y = rand (0, $ this-> height-imagefontheight ($ fontSize ));
Imagechar ($ this-> image, $ fontSize, $ x, $ y, $ char, $ fontColor );
}
}
/* Set interference elements in internal private methods */
Private function setDisturbColor ()
{
/* Draw interference points */
For ($ I = 0; $ I <$ this-> pixNum; $ I ++)
{
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color );
}
/* Draw interference lines */
For ($ I = 0; $ I <$ this-> lineNum; $ I ++)
{
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imageline ($ this-> image, rand (1, $ this-> width/2), rand (1, $ this-> height/2 ),
Rand ($ this-> width/2, $ this-> width-2), rand ($ this-> height/2, $ this-> height-2 ), $ color );
}
}
/* Enable session saving and use echo to output the image */
Function _ toString ()
{
$ _ SESSION ['code'] = strtoupper ($ this-> checkCode );
$ This-> getCreateImage ();
$ This-> createCheckCode ();
$ This-> setDisturbColor ();
$ This-> outputImg ();
}
/* Internal private method output image */
Private function outputImg ()
{
Header ("content-type: image/png ");
Imagepng ($ this-> image );
}
/* Destructor, release object */
Function _ destruct ()
{
Imagedestroy ($ this-> image );
}
}
?>

Imgcode. php output image

<?phpsession_start();require_once('myvcode.class.php');echo new Vcode();?>

Test.html: Same as img Tag reference

 

You can add a tag and use js to achieve another effect:

/* Partial refresh for verification code */
Function changeCode ()
{
Var imgcode = document. getElementById ('code ');
Var change = document. getElementById ('change ');
Change. onclick = function ()
{
/* You must add the following parameters to refresh */
Imgcode. src = 'Code. php? Tm '+ Math. random ();
}
}

Code and change are the IDs of img and a respectively.


Php image Verification Code

This is a phpcms Verification Code. It is very useful after over 10 thousand website experiences.
<? Php

Session_start ();

$ Enablegd = 1;
// Determine whether the image processing function exists
$ Funcs = array ('imagecreatetruecolor', 'imagecolorallocate', 'imagefill', 'imagestring', 'imageline ', 'imagerotate', 'imagedestroy', 'imagecolorallocatealpha ', 'imageellipse', 'imagepng ');
Foreach ($ funcs as $ func)
{
If (! Function_exists ($ func ))
{
$ Enablegd = 0;
Break;
}
}

Ob_clean (); // clear the buffer

If ($ enablegd)
{
// Create captcha
$ Consts = 'cdfgkmnpqrstwxyz23456 ';
$ Vowels = 'aek23456789 ';
For ($ x = 0; $ x <6; $ x ++)
{
$ Const [$ x] = substr ($ consts, mt_rand (0, strlen ($ consts)-1), 1); // obtain a random number in $ consts
$ Vow [$ x] = substr ($ vowels, mt_rand (0, strlen ($ vowels)-1), 1); // obtain a random number in $ vowels
}
$ Radomstring = $ const [0]. $ vow [0]. $ const [2]. $ const [1]. $ vow [1]. $ const [3]. $ vow [3]. $ const [4];
$ _ SESSION ['checkcode'] = $ string = substr ($ radomstring,); // display 4 characters

$ ImageX = strlen ($ radomstring) * 8; // Image Width
$ ImageY = 20; // Image Height
$ Im = imagecreatetruecolor ($ imageX, $ imageY); // create a true color image

// Creates two variables to store color
$ Background = imagecolorallocate ($ im, rand (180,250), rand (180,250), rand (180,250); // background color
$ ForegroundArr = array (imagecolorallocate ($ im, rand (0, 20), rand (0, 20), rand (0, 20 )),
Imagecolorallocate ($ im, rand (0, 20), rand (0, 10), rand (245,255 )),
Imagecolorallocate ($ im, rand (245,... the remaining full text>

After the php image verification code is inserted, write the judgment code when submitting the code ??

Action. php

<?
Session_start ();
$ Password = md5 (trim ($ _ POST ['Password']);
$ Str_reg = $ _ POST ['number']; // The verification code entered by the user
$ Str_reg = strtoupper ($ str_reg); // converts uppercase letters.

If ($ str_reg! = $ _ SESSION ['yzm'] or empty ($ str_reg ))
{Echo "Incorrect verification code ";
}
Else
{
$ _ SESSION ['yzm'] = ""; // clear the session
......
Perform database query, verify the user name and password
}
?>

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.