PHP verification code generation procedure _ PHP Tutorial

Source: Internet
Author: User
PHP verification code generation program methods. PHP verification code is a basic function of security verification when users register for logon or publish information, the following section describes some common PHP verification code generation and PHP verification code generation functions. This function is commonly used to provide a basic security verification function when users register for logon or publish information, the following small series will introduce some common PHP verification code generation code and application instances.

Example 1: Use arrays directly. this method is relatively simple.

The code is as follows:






$ Arr = array (2, 3, 4, 5, 8, 1, 9, 7, "a", "B", "c", "d", "e", "f ", "Medium", "country", "South", "North", "big", "small", "more", "less ");
$ B = array_rand ($ arr, 3 );

?>

Echo "code :";
Foreach ($ B as $ key)
{
Echo $ arr [$ key];
}
?>


(Save the preceding statement as a php file)

Header ("Content-Type: text/html; charset = utf-8 ");
Echo $ _ POST ["yanzhengma"];
Echo $ _ POST ["code"];
If ($ _ POST ["yanzhengma"] == _ POST ["code"])
{
Echo "the verification code is correct ";
}
Else
{
Die ("script" alert ('incorrect verification code "); location =" array_rand.php "; script");
}
?>

(Save the preceding statement as yz. php)


Example 2: Use arrays instead of a little more data.

The code is as follows:

Function UPCAbarcode ($ code ){
$ Lw = 2; $ hi = 100;
$ Lencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
$ Rencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
$ Ends = '20180101'; $ center = '20180101 ';
/* UPC-A Must be 11 digits, we compute the checksum .*/
If (strlen ($ code )! = 11) {die ("UPC-A Must be 11 digits .");}
/* Compute the EAN-13 Checksum digit */
$ Ncode = '0'. $ code;
$ Even = 0; $ odd = 0;
For ($ x = 0; $ x <12; $ x ++ ){
If ($ x % 2) {$ odd + = $ ncode [$ x];} else {$ even + = $ ncode [$ x];}
}
$ Code. = (10-($ odd * 3 + $ even) % 10) % 10;
/* Create the bar encoding using a binary string */
$ Bars = $ ends;
$ Bars. = $ Lencode [$ code [0];
For ($ x = 1; $ x <6; $ x ++ ){
$ Bars. = $ Lencode [$ code [$ x];
}
$ Bars. = $ center;
For ($ x = 6; $ x <12; $ x ++ ){
$ Bars. = $ Rencode [$ code [$ x];
}
$ Bars. = $ ends;
/* Generate the Barcode Image */
$ Img = ImageCreate ($ lw * 95 + 30, $ hi + 30 );
$ Fg = ImageColorAllocate ($ img, 0, 0, 0 );
$ Bg = ImageColorAllocate ($ img, 255,255,255 );
ImageFilledRectangle ($ img, 0, 0, $ lw * 95 + 30, $ hi + 30, $ bg );
$ Shift = 10;
For ($ x = 0; $ x if ($ x <10) | ($ x >=45 & $ x <50) | ($ x> = 85) {$ sh = 10;} else {$ sh = 0 ;}
If ($ bars [$ x] = '1') {$ color = $ fg;} else {$ color = $ bg ;}
ImageFilledRectangle ($ img, ($ x * $ lw) + 15, 5, ($ x + 1) * $ lw + 14, $ hi + 5 + $ sh, $ color );
}
/* Add the Human Readable Label */
ImageString ($ img, 4,5, $ hi-5, $ code [0], $ fg );
For ($ x = 0; $ x <5; $ x ++ ){
ImageString ($ img, 5, $ lw * (13 + $ x * 6) + 15, $ hi + 5, $ code [$ x + 1], $ fg );
ImageString ($ img, 5, $ lw * (53 + $ x * 6) + 15, $ hi + 5, $ code [$ x + 6], $ fg );
}
ImageString ($ img, 4, $ lw * 95 + 17, $ hi-5, $ code [11], $ fg );
/* Output the Header and Content .*/
Header ("Content-Type: image/png ");
ImagePNG ($ img );
}

UPCAbarcode ('20140901 ');

?>

Example 3: A Complete ajax refresh verification code instance


Vcode. php

The code is as follows:

Session_start (); // enable the session function
Header ("Cache-Control: no-cache, must-revalidate ");

$ Im = imagecreate (60, 30); // you can specify the image width and height.
$ Vcode = getVCode (); // get the characters to be displayed
$ Bg = imagecolorallocate ($ im, 255,255,255); // define the image background
$ Txt = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // defines the color of the characters to be displayed
Imagestring ($ im, 8, 0, 0, $ vcode, $ txt); // write a string to the image
Header (Content-type: image/jpeg); // Define Content-type
Imagejpeg ($ im); // display images in JPEG format
$ _ SESSION [vcode] = $ vcode; // write to the SESSION

Function getVCode () {// randomly generates a specified number of strings.
$ Codenum = 4;
$ Checkcode = "";
$ String = ""; // optional string to be displayed, please define it yourself;
For ($ I = 0; $ I <$ codenum; $ I ){
$ Number = rand (0, 2 );
Switch ($ number) {// can be flexibly defined based on optional strings;
Case 0: $ rand_number = rand (0, 10); break;
Case 1: $ rand_number = rand (11, 36); break;
Case 2: $ rand_number = rand (37,62); break;
}
$ Code = substr ($ string, $ rand_number, 1 );
$ Checkcode = $ checkcode. $ code;
}
Return $ checkcode;
}
?>


Loginform.html

The code is as follows:



Vcode. js

The code is as follows:

// This function is used to obtain the verification code.

Function getVCode (){
Var vcode = document. getElementById ('vcode ');
Vcode. src = 'vcode. php? Nocache = '+ new Date (). getTime ();
}

// This function is used to verify the verification code
Function usrVCode (){
If (! CheckLogin () return false;
Var loginvcode = document. loginform. loginvcode. value;
Var xmlhttp1 = createAjax ();
Var data = '& loginvcode =' + loginvcode;
If (xmlhttp1 ){
Var state = document. getElementById ('state ');
Xmlhttp1.open ('GET ',? Do = vcodedo '+ data, true );
Xmlhttp1.send (null );
Xmlhttp1.onreadystatechange = function (){
If (xmlhttp1.readyState = 4 & xmlhttp1.status = 200 ){
SetTimeout ("state. style. display = 'none';", 1000 );
Var myres = xmlhttp1.responseText;
Var result = (myres = 1 )? "Congratulations, the verification code is entered correctly! ":" Sorry, incorrect verification code! ";
If (myres = 0) alert (result );
If (myres = 1) usrLogin ();
}
Else {
State. style. display = "";
State. style. left = (document. body. offsetWidth-350)/2;
State. style. top = (document. body. offsetHeight-235)/2 + document. body. scrollTop;
}
}
}
}

...

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.